I'm new to Ruby on Rails world.
I noticed there is at least one way to access controller instance variables from a test case.
Indeed, suppose this test method:
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:products)
end
products is an instance variable contained within concerned controller. And for sure, test case has reference to this controller. So assigns() method uses it to inspect a hash of controller's instance variables and thus allows to access any precised object from any other files that previously called an action to this controller.
So I wonder two questions:
Why do not create a 'Binding' to controller instead of using assigns() method?
I imagine a version where it is possible to do:
test "should get index" do
get :index
assert_response :success
assert_not_nil @products
end
Wouldn't it be shorter and cleaner?
Binding is the mechanism that allows ERB file to access controller instance variables, as shows this links:
http://rrn.dk/rubys-erb-templating-system
What isn't this mechanism applicable to Test case ? Is assigns() method essential ?
If you brought over the binding, though, this might pass and should not
test "should get index" do
@fake_products = [1,2,3]
get :index
assert_response :success
assert_not_nil @fake_products
end
You wouldn't necessarily want all instance variables in your test to combine with instance variables in your controller. Assigns lets you 'scope' your assertions to just the controller instance variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With