Reputation: 6217
A minitest returns Expected nil to be truthy.
The controller action processes the following statements
@cu = current_user.roleshopusers.where(shop_id: @site.shop_id).first
@cu_role = @cu.try(:role_id)
@cu_buyer = @cu.buyer
Rails.logger.info @cu.inspect
Rails.logger.info @cu.buyer
And the test log returns:
#<Roleshopuser id: 434940726, user_id: 7, shop_id: 31, role_id: 1, buyer: true, special_support: false, special_needs: nil, notes: nil, discount: 0.0, created_at: "2021-06-16 13:56:37.288691000 +0200", updated_at: "2021-06-16 13:56:37.288691000 +0200">
true
but the test, while returning correct values with the same method definition,
puts current_user.id
puts current_user.roleshopusers.where(shop_id: @site.shop_id).first.buyer
puts @cu.inspect
assert @cu_buyer
has no instance variable available to it.
7
true
nil
How should this test be stated, then?
Upvotes: 1
Views: 526
Reputation: 102368
Instance variables are not globals. They are lexical variables scoped to the instance where the are defined. This is a common beginner hangup if you start with Rails without learning Ruby first as it "magically" makes your controller instance variables available to the view.
Your expectation that the instance variable you have defined in your controller should be set in the test is thus completely false as they are completely separate instances of different classes.
In legacy controller tests the assigns
method was commonly used to poke inside of the controller and write assertions about its instance variables:
assert(assigns(:cu_buyer))
This was deprechiated and removed entirely from Rails, but still exists as an gem. Its use is not recommended outside of legacy code. Instead the modern approach is to write integration tests that test the response and effects of your controller without poking into its internals.
Upvotes: 2