Ruby 1.9.3,Rails 3.2.2 我正在尝试写一个Rpsec(Capybara)来测试我的页面是否正确地复制了“Post”这个词。运行我的Rspec时出现错误:
c:/.../ my_app / spec / requests / user_pages_spec.rb:58:在`block(3 levels)in':未定义的局部变量或方法'user'for ...(NameError)
以下是相关测试:
describe "profile page" do
let(:user){FactoryGirl.create(:user)}
let!(:m1){FactoryGirl.create(:micropost, user: user, content: "Food")}
let!(:m2){FactoryGirl.create(:micropost, user: user, content: "Bar")}
before {visit user_path(user)}
it {should have_selector('h1', text: user.name)}
it {should have_selector('title', text: user.name)}
describe "pagination" do
before(:all){40.times {FactoryGirl.create(:micropost, user: user, content: "Food")}}
after(:all){User.delete_all}
it {should have_selector('div.pagination')}
end
describe "microposts" do
it {should have_content(m1.content)}
it {should have_content(m2.content)}
it {should have_content(user.microposts.count)}
before do
sign_in user
visit root_path
User.delete_all
end
it "should pluralize post numbers" do
FactoryGirl.create(:micropost, user: user, content: "Food")
page.should have_content("1 micropost")
2.times {FactoryGirl.create(:micropost, user: user, content: "Food")}
page.should have_content("2 microposts")
end
我不确定我是否正在以正确的方式测试帖子的多元化,但我主要是为什么该块无法“看到”用户对象,因为我的if之后的行声明可以看到它。如果我注释掉if块,一切运行正常。
答案 0 :(得分:1)
it
之外的任何代码都无法访问使用before
,let
等定义的变量。您需要将其置于it
调用中,用一个描述总if / else测试的文本参数说。您是否可以更新此问题以包含规范范围内的其他代码?
答案 1 :(得分:1)
个人资料页面没有多个要测试的内容。 测试应该是static_pages #home。
describe 'Home page' do
describe 'for signed-in users' do
let(:user) { FactoryGirl.create(:user) }
before do
FactoryGirl.create(:micropost, user: user, content: 'Lorem ipsum')
FactoryGirl.create(:micropost, user: user, content: 'Dolor sit amet')
sign_in user
visit root_path
end
it { should have_content('micropost'.pluralize(user.microposts.count)) }
end
end