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)

これは、「 describe micropost delete links 」ブロックを追加した後にのみ発生します。問題は何ですか?

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 に答える