我是Rails的新手,所以我可能在这里完全错过了这个问题,但是在使用引用了我自己的Page类的测试时,在Capybara中似乎出现了问题。
当我进行的测试创建了Page类的实例(通过FactoryGirl)时,则无法运行任何Capybara页面方法(例如visit,fill_in,click等)。我收到以下错误:
NoMethodError:
undefined method `visit' for #<Page:0x007fdf5eb9d990>
当我添加调试器语句并检查page
变量时,它是我的Page类的一个实例。当我要执行Capybara方法时,如何确保我始终使用Capybara页面?
我的功能规格(我知道这是不完整的,但是该错误发生在before循环中):
require 'spec_helper'
feature 'Modifying a page' do
let(:account) { FactoryGirl.create(:account) }
let!(:page_creator) { FactoryGirl.create(:user, :account => account) }
let!(:standard_user) { FactoryGirl.create(:user, :account => account) }
let!(:site) { FactoryGirl.create(:site, :account => account) }
let!(:page) { FactoryGirl.create(:page, :account => account, :site => site, :creator => page_creator) }
context 'as the creator of the page' do
before(:each) { sign_in_via_form account, page_creator, page_creator.password }
scenario 'with valid information' do
new_page_name = Faker::Lorem.characters(Random.rand(10..100))
new_page_url = Faker::Internet.url
new_page_description = Faker::Lorem.characters(Random.rand(50..250))
click_link site.name
click_link 'Pages'
find("a[href='#{edit_site_page_path([site, page])}'").click
end
end
end
该sign_in_via_form
方法在以下文件中定义:
module AuthenticationHelpers
include Warden::Test::Helpers
Warden.test_mode!
def sign_in_via_form(account, user, password)
visit "http://#{account.subdomain}.example.com"
fill_in 'Email', :with => user.email
fill_in 'Password', :with => password
click_button 'Sign In'
end
end
我还有许多其他功能可与正在运行的不同模型配合使用(并且使用上述sign_in_via_form
方法不会出现任何问题,这就是我认为这与Page类有关)的原因。
就像我说的那样,我是Rails的新手,所以如果这些测试看起来有些难看,您就必须原谅我:-)
谢谢你的帮助
好的,它是如此简单,以至于我错过了真正的问题。
我使用let
名为的方法创建了一个变量,该方法page
显然覆盖了Capybara页面变量。我将其更改为以下内容,并且一切都按预期运行(请注意变量the_page
):
feature 'Modifying a page' do
let(:account) { FactoryGirl.create(:account) }
let!(:page_creator) { FactoryGirl.create(:user, :account => account) }
let!(:standard_user) { FactoryGirl.create(:user, :account => account) }
let!(:site) { FactoryGirl.create(:site, :account => account) }
let!(:the_page) { FactoryGirl.create(:page, :account => account, :site => site, :creator => page_creator) }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句