【发布时间】:2012-10-18 20:21:54
【问题描述】:
我正在使用 minitest 与 factory girl 和 capybara 进行集成测试。当我不使用工厂女孩创建用户对象时,Capybara 工作正常,如下所示:
it "logs in a user successfully" do
visit signup_path
fill_in "Email", :with => "joey@ramones.com"
fill_in "Password", :with => "rockawaybeach"
fill_in "Password confirmation", :with => "rockawaybeach"
click_button "Create User"
current_path == "/"
page.text.must_include "Signed up!"
visit login_path
fill_in "Email", :with => "joey@ramones.com"
fill_in "Password", :with => "rockawaybeach"
check "Remember me"
click_button "Log in"
current_path == "/dashboard"
page.text.must_include "Logged in!"
page.text.must_include "Your Dashboard"
end
但是,一旦我尝试使用 factory girl 创建用户,就会发生奇怪的事情,例如 visit 方法和 click_button 方法停止工作。例如,这个测试似乎没有任何问题:
require "test_helper"
describe "Password resets" do
before(:each) do
@user = FactoryGirl.create(:user)
end
it "emails user when requesting password reset" do
visit login_path
click_link "password"
fill_in "Email", :with => user.email
click_button "Reset my password"
end
end
这是我的 factory.rb:
FactoryGirl.define do
factory :user do |f|
f.sequence(:email) { |n| "foo#{n}@example.com" }
f.password "secret"
f.password_confirmation "secret"
end
end
这是我得到的实际错误:
est_0001_emails user when requesting password reset 0:00:01.624 ERROR
undefined local variable or method `login_path' for #<#<Class:0x007fc2db48d820>:0x007fc2df337e40>
但是,如果我删除 @user = FactoryGirl.create(:user),visit login_path 可以正常工作
这是 Capybara 的错误吗?还是我在这里做错了什么?
【问题讨论】:
-
如果将测试中的 before 块替换为 let(:user) { Factory(:user) } 会发生什么?
-
得到同样的错误。 Capybara 似乎只是没有提交表单。
-
这条线有问题吗?
fill_in "Email", :with => user.email不应该是@user.email吗? -
另外,请尝试在没有
do |f|和f.前缀的情况下定义您的工厂。这似乎不再是标准做法了。 -
用
let(:user) { Factory(:user) }替换前块无效。仍然遇到同样的问题。我在测试登录过程时也遇到了类似的问题。我将粘贴下面发生的事情,因为我认为它可能是相关的。我应该在上面的代码中使用@user.email,但这并没有帮助。我还删除了do |f|无济于事。
标签: ruby-on-rails testing capybara factory-bot minitest