水豚 rails 测试错误

Capybara rails testing error

我是 rails 的新人。我正在使用 copybara gem 来测试该设计。这是一个测试代码

require 'test_helper'

class UserTasksTest < ActionDispatch::IntegrationTest
  test 'Should create a new user' do
    visit root_url
    click_link 'Sign up'
    fill_in "Email", with: 'capybaratest@test.ru'
    fill_in "Password", with: 'capybara'
    fill_in "Password confirmation", with: 'capybara'
    click_button 'Sign up'
    within("h1") do
      assert has_content?(user.email)
    end
  end
end

运行 测试后出现错误:

undefined local variable or method `user'

如何正确编写测试?

您正在测试新用户的创建,并希望在注册后显示其电子邮件。因此,未定义 user 变量,因此您会收到此错误。尝试以下操作:

require 'test_helper'

class UserTasksTest < ActionDispatch::IntegrationTest
  test 'Should create a new user' do
    visit root_url
    click_link 'Sign up'
    fill_in "Email", with: 'capybaratest@test.ru'
    fill_in "Password", with: 'capybara'
    fill_in "Password confirmation", with: 'capybara'
    click_button 'Sign up'
    within("h1") do
      assert has_content?('capybaratest@test.ru')
    end
  end
end

澄清一下,您将使用 user 变量的一种情况是登录流程:在开始测试之前,您将创建一个有效用户并使用此设置 user 变量新用户...通过这种方式,您可以使用用于创建用户的电子邮件和密码填写 email/password 字段,最后检查它是否显示,即 "Welcome #{user.name}".