0

我在 rspec 中有以下测试块:

    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

      describe "sidebar" do
        it { should have_selector('div.pagination')}
        it "should render the user's feed" do
          user.feed.each do |item|
            page.should have_selector("li##{item.id}", text: item.content)
          end
        end

        describe "micropost delete links" do
          let(:other_user) { FactoryGirl.create(:user, email: "other@example.com") }
          let!(:m3) {  FactoryGirl.create(:micropost, user: other_user, content: "Other MP") }

          user.feed.each do |item|
            find("li##{item.id}").should have_link('delete')
          end
        end

        it "should show the correct number of microposts" do
          page.should have_selector('span', text: '2 microposts')
          user.microposts.first.destroy
          visit root_path
          page.should have_selector('span', text: '1 micropost')
          user.microposts.first.destroy
          visit root_path
          page.should have_selector('span', text: '0 microposts')
        end
      end
    end

当我运行它时,我收到此错误:

/Users/8vius/Projects/RubyDev/sample_app/spec/requests/static_pages_spec.rb:49:in `block (5 levels) in <top (required)>': undefined local variable or method `user' for #<Class:0x007fd45c8c39c0> (NameError)

这只发生在我添加了“描述微帖子删除链接”块之后,知道问题是什么吗?

4

1 回答 1

2

您有一个在块外运行测试的描述it块。因此user未定义。

你的:

describe "micropost delete links" do
  let(:other_user) { FactoryGirl.create(:user, email: "other@example.com") }
  let!(:m3) {  FactoryGirl.create(:micropost, user: other_user, content: "Other MP") }

  user.feed.each do |item|
    find("li##{item.id}").should have_link('delete')
  end
end

与:

describe "micropost delete links" do
  let(:other_user) { FactoryGirl.create(:user, email: "other@example.com") }
  let!(:m3) {  FactoryGirl.create(:micropost, user: other_user, content: "Other MP") }

  it "should have delete link" do
    user.feed.each do |item|
      find("li##{item.id}").should have_link('delete')
    end
  end
end
于 2012-08-24T20:11:26.667 回答