用户注册集成测试不在数据库中插入值

问题描述 投票:0回答:1

我是rails测试的新手,我已经为用户注册编写了集成测试。测试工作正常,但它没有在数据库中插入记录。

这是代码

require 'test_helper'

class SignupTest < ActionDispatch::IntegrationTest
  test "user signup" do
    visit new_user_registration_path
    fill_in "user_email",    with: "[email protected]"
    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

这是我用来运行测试的命令

rake test:integration

当我运行测试时,结果是

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

我还检查了日志,但日志中没有任何内容。

这是我的测试gemlist

group :test do
  gem 'capybara'
  gem 'capybara-webkit'
  gem 'vcr'
  gem 'webmock'
  gem 'launchy'
end
ruby-on-rails ruby integration-testing
1个回答
1
投票

每次测试运行后都会清理测试数据库,因此在运行测试套件后将无法看到任何记录(根据清理方法,即使在测试期间,如果使用的话,您也无法在数据库中看到任何内容与DB的不同连接)。

如果要测试该用户是否已保存,则需要在集成测试中进行测试。例如

# ...
assert_text "Welcome! You have signed up successfully."
assert(User.where(email: "[email protected]").exists?)

或者事件更好 - 写一个检查它的单元测试。

Related answer

© www.soinside.com 2019 - 2024. All rights reserved.