0

Ruby 1.9.3, Rails 3.2.2 I'm trying to write an Rpsec (Capybara) to test if my page is properly pluralizing the word "Post". I get an error when running my Rspec that states:

c:/.../my_app/spec/requests/user_pages_spec.rb:58: in `block (3 levels) in ': undefined local variable or method 'user' for ... (NameError)

Here's the relevant test:

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

I'm not sure if I'm going about testing for pluralization of posts the right way either, but I'm mainly stumped about why that block can't "see" the users object since the line right after my if statement can see it. if I comment out the if block everything runs fine.

4

2 回答 2

1

外部的任何代码it都无法访问使用 , 等定义的变量beforelet您需要将其放在it调用中,例如使用描述总 if/else 测试的文本参数。您能否更新此问题以包含范围内的规范中的其他代码?

于 2013-06-22T22:56:12.003 回答
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
于 2014-08-27T08:12:09.580 回答