调用utility_rb中的sign_up实用程序测试方法失败
问题描述:
我在Rails上编写测试方面有点新,我正在跟着RoR教程。我试图添加我自己的测试实用程序方法sign_up,类似于utility.rb中已有的sign_in方法。当调用它对着我的注册页面,不过,我得到这个错误:调用utility_rb中的sign_up实用程序测试方法失败
2) User pages index pagination as an admin user to unlock new users
Failure/Error: sign_up user
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or label 'Name' found
# (eval):2:in `fill_in'
# ./spec/support/utilities.rb:33:in `sign_up'
# ./spec/requests/user_pages_spec.rb:63:in `block (5 levels) in <top (required)>'
我sign_up方法是这样的:
def sign_up(user)
visit signup_path
fill_in "Name", with: user.name
fill_in "Email", with: user.email
fill_in "Password", with: user.password
fill_in "Confirmation", with: user.password
click_button "Sign up"
end
它似乎开始出问题甚至只是逛signup_path - 我甚至不确定它会去那里。此外,如果我将fill_in "Name"
行注释掉,它将以相同的方式扼住fill_in "Email"
行。
关于这里发生了什么的任何建议或想法将不胜感激。
感谢, -Matt
答
灯泡终于去上。
当您使用工厂创建用户时,它会将其插入到数据库中。所以,当您进入注册页面并尝试使用该用户时,它已经存在,您将被重定向到主页。这意味着没有任何字段可以与fill_in一起使用。简化规格:
require 'spec_helper'
describe "signup page" do
subject { page }
describe "with valid information" do
before { sign_up('em', '[email protected]', '123456') }
it { should have_title_and_h1('em') }
end
end
实用方法:
def sign_up(name, email, password)
visit signup_path
fill_in "Name", with: name
fill_in "Email", with: email
fill_in "Password", with: password
fill_in "Confirmation", with: password
click_button "Create my account"
end
将创建通过浏览器界面的用户。