0

I am running Rails 3.2.11 and believe I have minitest spec and capybara installed and working correctly through my gem file and test_helper.rb

I have the following integration test:

describe "Products integration" do
fixtures :users, :businesses
  it "shows product's name" do
    visit root_path
    fill_in "email", :with => users(:fu).email
    fill_in "password", :with => "secret"
    click_button "Log In"

    assert_equal products_path, current_path
  end
end

With the browser, once the user is logged in they are redirected to the products_path which displays all of their products.

My sample user fixture does not have any products setup. Therefore in my index.html.erb the line <% if @products.present? %> should be false and no row data should be attempted to be shown. This is not the case and my test fails with Error: undefined method type_name for nil:NilClass, as it appears to attempt to write the table data <td><%= product.product_type.type_name %></td>

The action that leads to index.html.erb only contains @products = Product.includes(:product_type)

Why is the test returning true for products.present? and hence fail?

Marklar
  • 1,247
  • 4
  • 25
  • 48

1 Answers1

0

Ah, but there is at least one product somehow, otherwise your error would be

Error: undefined method product_type for nil:NilClass

So there was a non-nil product returned that had a nil product_type.

Try putting a

puts @products.inspect

in your spec and maybe that will give a clue where the product is being created.

Also, if product_type should never be nil, put a NOT NULL constraint on that column in the db to really ensure that such shenanigans are not possible. Validations at the ActiveRecord level are useful, but can be circumvented in a lot of ways and can't be relied upon for actual data integrity purposes.

Chris Aitchison
  • 4,656
  • 1
  • 27
  • 43
  • Thank you for your help. `puts` doesn't work for me because as soon as the error is thrown it stops the test. I am using multitenancy through scopes and fixtures, I have a feeling it may be an issue with how I am using fixtures and relations. I will continue to look into it but in the meantime I will change my view to allow null, you have highlighted to me that this is an issue. Thank you. – Marklar Apr 12 '13 at 00:39
  • Actually, multitenancy scoping wasn't the problem at all. With fixing the null product_type as you mentioned I was then able to use `puts @products.inspect` it returns nil, even though when I use launchy to open the web there are products present. Hmm.... – Marklar Apr 12 '13 at 01:18
  • If a ````puts @products.inspect```` returns ````nil````, but a ````@products.present?```` returns ````true```` then you have come across a very peculiar bug. – Chris Aitchison Apr 12 '13 at 02:16