2016-10-25 96 views
0

我是新的导轨。我使用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: '[email protected]' 
    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'

应该如何我正确地写测试?

回答

1

您正在测试新用户的创建,并希望在注册后显示其电子邮件。所以,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: '[email protected]' 
    fill_in "Password", with: 'capybara' 
    fill_in "Password confirmation", with: 'capybara' 
    click_button 'Sign up' 
    within("h1") do 
     assert has_content?('[email protected]') 
    end 
    end 
end 

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

+0

谢谢,真的对我有帮助:) –