Rail Integration Testing Issues

I am trying to do integration testing using my own Rails testing system, in a user model, as shown in the rail guide.

In routes.rb, I did the following mapping:

match '/signup', to: 'users#new' 

The test is as follows:

 class UserStoriesTest < ActionDispatch::IntegrationTest test "sign up" do User.delete_all post_via_redirect "/users", user: {name: 'david', email: ' david@example.com ', password: 'qwe123', password_confirmation: 'qwe123'} assert_response :success users = User.all assert_equal 1, users.size, users.size end end 

No matter what I do, I cannot force him to create a new record, so the second statement fails. I also tried to compromise it by setting the wrong confirmation password, and this does not lead to the first statement. I tried only the post instead of post_via_redirect or using the path "signup" instead of "/ users" without changes.

Of course, I'm doing something stupid. What am I missing here?

Sincerely.

+4
source share
2 answers

In fact, you are not filling out the registration form. It looks like you are using test :: unit, what you need, webrat or capybara to handle request specifications, I recommend them.

https://github.com/jnicklas/capybara

https://github.com/brynary/webrat

0
source

This answer is probably too old to be very useful for you, but it seems to me that I like the pair.

1) This should not be an integration test, since you are not testing the interaction between several controllers. This should be a functional test that is used to test individual controllers, in this case your UserController. This is important because various methods and functions are available or loaded depending on your ad.

 class UserStoriesTest < ActionDispatch::IntegrationTest 

You will get a completely different environment than

 class UserTest < ActiveSupport::TestCase 

Note that the integration test is part of ActionDispatch, and the functional test is part of ActiveSupport. I highly recommend translating this to a functional test, and this is probably the source of your errors.

2) Using delete_all is a bit extreme. In fact, you can just run the assert test when you do your post to find out if there is a difference in the score.

 assert_difference('User.count') do post :create, :user => {name: 'david', email: ' david@example.com ', password: 'qwe123', password_confirmation: 'qwe123'} end 

This will claim that User.count is different from how you run what is in the do loop and after it completes. This block can replace everything that you have in your test "Register". For fun, you can change assert_difference to assert_no_difference, and if that passes, you know that something is wrong with your post method in your user_controller.rb (this is where I assume your problem actually lies).

3) The assert_equal line looks a bit strange. The last argument passed, this is the message that it will spit out on error. Listing only a number may work for one test, but it will look very strange in many tests. Something like the following, in my opinion, closer to the norm:

 assert_equal 1, User.count, "Expected User.count to be equal to 1, instead it is equal to: " + User.count.to_s 

Also note that you can simply use ModelName.count instead of doing something like putting all the records in an object and then calling the size of that object. It can become very dirty, very fast.

Finally, as a general debugging tool, you can throw puts statements or sign-ups everywhere to see what really happens.

 puts User.count 

After your delete_all and your post_via_redirect tell you very quickly what is going wrong. if you use the logger method, it will be logged in your appname / log / ENV.log. Therefore, if you use tests, it will be in test.log.

 logger.debug User.count 

Your logs will also store all requests, so you can read and see if you get 200 OK, 302 redirects, etc.

I suggest checking out the following guides provided by the rails, these are great starting points:

http://guides.rubyonrails.org/testing.html

http://guides.rubyonrails.org/debugging_rails_applications.html

Hope this helps someone!

0
source

Source: https://habr.com/ru/post/1403771/


All Articles