Reputation: 11
Ruby On Rails Tutorials, Listing 10.31 Listing 10.31: Adding account activation to the user signup test.
I get nil instead of a valid user on this line: user = assigns(:user)
The codes are here:
....
class UsersSignupTest < ActionDispatch::IntegrationTest
test "valid signup information" do
assert_difference 'User.count', 1 do
post_via_redirect users_path, user: { name: "testA",
email: "[email protected]",
password: "foobar",
password_confirmation: "foobar" }
end
assert_equal 1, ActionMailer::Base.deliveries.size
debugger
user = assigns(:user)
....
def create
@user = User.new(user_params)
if @user.save
UserMailer.account_activation(@user).deliver_now
flash[:info] = "Please check your email to activate your account."
redirect_to root_url
else
render 'new'
end
end
The dubug info shows there is no :user in the assigns hash:
Run options: --seed 10699
# Running:
..............
[35, 44] in /home/artreal/Codes/hello_world/test/integration/users_signup_test.rb
35:
36: assert_equal 1, ActionMailer::Base.deliveries.size
37:
38: debugger
39:
=> 40: user = assigns(:user) #assigns (a hash) lets us access instance variables in controllers
41:
42: assert_not user.activated?
43: # Try to log in before activation.
44: log_in_as(user)
(byebug) p assigns
{"marked_for_same_origin_verification"=>true}
{"marked_for_same_origin_verification"=>true}
(byebug)
And here is the error:
1) Error:
UsersSignupTest#test_valid_signup_information:
NoMethodError: undefined method `activated?' for nil:NilClass
test/integration/users_signup_test.rb:42:in `block in <class:UsersSignupTest>'
The routes seems to be correct:
...
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
...
The API document of ActionController::TestCase (can't post a link) has a section on the assign hash. But the API doc of ActionDispatch::IntegrationTest (can't post a link) does not say anything on the same hash.
What did I miss on using assign in integration test?
Upvotes: 1
Views: 224
Reputation: 11
Found the problem.
In the integration test, I used the line:
post_via_redirect users_path, user: { name: "testA",
email: "[email protected]",
password: "foobar",
password_confirmation: "foobar" }
Instead, the tutorial is using a simple post
function.
post users_path, user: { name: "testA",
email: "[email protected]",
password: "foobar",
password_confirmation: "foobar" }
Is it because the redirect part removed values in the assign
hash?
Upvotes: 0