0

I am new in rails testing and I have written an integration test for user signup. Test is working fine but it is not inserting record in the database.

Here is the code

require 'test_helper'

class SignupTest < ActionDispatch::IntegrationTest
  test "user signup" do
    visit new_user_registration_path
    fill_in "user_email",    with: "abc@gmail.com"
    fill_in "user_password", with: "password"
    fill_in "user_password_confirmation", with: "password"
    click_button "Sign up"
    assert_text "Welcome! You have signed up successfully."
  end
end

This is the command that I am using to run the test

rake test:integration

When I run the test the results are

Run options: --seed 62721

# Running:

.

Finished in 3.116669s, 0.3209 runs/s, 0.3209 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

I have also checked the logs but nothing in the log.

This is my gemlist for testing

group :test do
  gem 'capybara'
  gem 'capybara-webkit'
  gem 'vcr'
  gem 'webmock'
  gem 'launchy'
end
mrzasa
  • 22,895
  • 11
  • 56
  • 94
john
  • 611
  • 1
  • 7
  • 33

1 Answers1

1

Test database is cleaned up after each test run so you will not be able to see any records after you run your test suite (depending on the cleaning method you won't be able to see anything in the DB even during the test if you use different connection to the DB).

If you want to test that user was saved, you need to do it inside the integration test. For example

# ...
assert_text "Welcome! You have signed up successfully."
assert(User.where(email: "abc@gmail.com").exists?)

Or event better - write a unit test that checks it.

Related answer

mrzasa
  • 22,895
  • 11
  • 56
  • 94
  • This is what I have got after inserting your code Run options: --seed 25384 # Running: . Finished in 3.650668s, 0.2739 runs/s, 0.5478 assertions/s. 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips Is this correct result? – john Mar 13 '18 at 13:50
  • Can you please guide me that how to run a single test currently when I enter this command rails test:integration all the tests start running. – john Mar 13 '18 at 13:51
  • Yup, see that assertion number increased - that's the one for the user. You can also try to make the test fail (e.g. by commenting some parts of the production code. Regarding running a single test, see [this answer](https://stackoverflow.com/a/1507074/580346) – mrzasa Mar 13 '18 at 13:53
  • no it is not working ruby -I test test/integration/signup_test.rb -n signup result is Run options: -n signup --seed 46418 # Running: Finished in 0.002650s, 0.0000 runs/s, 0.0000 assertions/s. 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips – john Mar 13 '18 at 13:56
  • it's rather `ruby -I test test/integration/signup_test.rb -n "user signup"` – mrzasa Mar 13 '18 at 13:58
  • same results 0 0 0 0. – john Mar 13 '18 at 14:00