ToTenMilan
ToTenMilan

Reputation: 640

Failing (previously passing) RSpec test even after commiting back

I am following the book APIs on Rails. I am in chapter 8. While testing with RSpec 3.6 I am getting errors which I wasn't getting before. I am getting them even when I git checkout to the commit in which these tests were passing. I am sure they were, because I am writing a blog post along, about following this book in Rails 5.1.3. I discovered that they are not passing, by running whole test suite at the end of chapter 8(failing specs come from chapter 7 and 6). Before I was running specs mostly for specific files.

So I thought that this problem may be system dependent, but the only thing I was doing in terminal outside of the app was:

$ swapoff -a
$ swapon -a

and maybe sudo apt-get update

So I am in a bind now.

THE ERROR:

The case with this error is that FactoryGirl is creating 4 instances of product while test is getting 6 after the GET request to #index action

The products_controller_spec.rb:

require 'rails_helper'

RSpec.describe Api::V1::ProductsController, type: :controller do
  ...

  describe "GET #index" do
    before(:each) do
      4.times { FactoryGirl.create :product }
    end

    context "when is not receiving any product_ids parameter" do
      before(:each) do
        get :index
      end

      it "returns 4 records from the database" do
        products_response = json_response[:products]
        expect(products_response).to have(4).items
      end

      ...
    end
    ...
  end
...
end

the error message:

Api::V1::ProductsController GET #index when is not receiving any product_ids parameter returns 4 records from the database
     Failure/Error: expect(products_response).to have(4).items
       expected 4 items, got 6

If I puts products_response before failing test I got json with these six records:

[
  {
    "id": 1,
    "title": "Audible Remote Amplifier",
    "price": "100.0",
    "published": false,
    "user": {
      "id": 1,
      "email": "[email protected]",
      "created_at": "2017-08-27T12:13:26.977Z",
      "updated_at": "2017-08-27T12:13:26.977Z",
      "auth_token": "KA1ugPyXzXsULh6rN6QX",
      "product_ids": [
        1
      ]
    }
  },
  {
    "id": 2,
    # rest of attributes
  },
  {
    "id": 3,
    # rest of attributes
  },
  {
    "id": 4,
    # rest of attributes
    }
  },
  {
    "id": 5,
    # rest of attributes
  },
  {
    "id": 6,
    # rest of attributes
  }
]

TESTING MANUALLY:

My app seems work fine. While hitting the api.mydomain.dev/products, I got nice json response after creating 3 products in rails console:

{
  "products": [
    {
      "id": 1,
      "title": "product",
      "price": "3.3",
      "published": false,
      "user": {
        "id": 1,
        "email": "[email protected]",
        "created_at": "2017-08-13T07:31:29.339Z",
        "updated_at": "2017-08-27T13:09:06.948Z",
        "auth_token": "HJLb-d4DpgxQDzkR1RDf",
        "product_ids": [
          1,
          2
        ]
      }
    },
    {
      "id": 2,
      # attrs of second product in json
    },
    {
      "id": 3,
      # attrs of third product in json
    }
  ]
}

and also with params in URL like api.mydomain.dev/products?min_price=15&max_price=30 (which btw are the two of the rest of failing specs not included here):

{
  "products": [
    {
      "id": 3,
      "title": "ps two",
      "price": "20.0",
      "published": false,
      "user": {
        "id": 2,
        "email": "[email protected]",
        "created_at": "2017-08-27T12:50:58.905Z",
        "updated_at": "2017-08-27T12:50:58.905Z",
        "auth_token": "YynJJeyftTEX49KU1-qL",
        "product_ids": [
          3
        ]
      }
    }
  ]
}

I have 4 more errors in spec/models/product_spec.rb but to keep this question short I won't include them here. I think the reason is the same and this error is most concise.

I may provide more code, however it's public on my github repo, e.g. app/models/product.rb or app/api/v1/products_controller.rb

summarizing:

The app is working fine and provide expected output while testing manually. I got the errors while testing automatically, even after committing back to the place where these tests were passing. This may suggest that problem is on the system side. If it's not on the system side, why it's getting extra two objects while it was getting four before? Where is the problem?

Upvotes: 0

Views: 787

Answers (1)

Sarmad Sabih
Sarmad Sabih

Reputation: 166

It could be that your Test DB is not being cleaned. Try running rails c test and query Product.count to see if the Test DB has the product records which may be causing this error. You can also put a byebug or puts Product.count in your before(:each) block before using the factory to create records to ensure that there ain't no existing records before running the tests. If that's the case, you'll need to clean your DB before and after tests manually or by the DatabaseCleaner Gem.

Tip: It's also better to use a variable for the number of records you're creating and expecting. Example:

products_count = 4.times { FactoryGirl.create :product }
expect(products_response).to have(products_count).items

Upvotes: 1

Related Questions