在rspec之前嵌套不会被执行

时间:2013-12-04 08:02:46

标签: ruby-on-rails rspec

我正在尝试执行以下操作:

require 'spec_helper'

describe Artwork do
  before do
     @artwork = Artwork.new(name:  "foo", ...)
  end

  subject { @artwork }

  it { should respond_to(:name) }

  describe "like should be in effect" do
    before do
      this_should_raise_an_error
      @artwork.save!
      let(:liked_user1) { FactoryGirl.create(:user) }
      let(:liked_user2) { FactoryGirl.create(:user) }
    end
    expect(@artwork.liked_users_count).to eq(0)
  end
end

this_should_raise_an_error字符串不会引发任何事情(意味着前一个块没有被执行?) 无论有没有我得到 -

undefined method 'liked_users_count' for nil:NilClass  

根本不会发生这种情况(我应该在@artwork中有所作为)

我在这里遗漏了一些基本的东西,但我似乎无法弄清楚

1 个答案:

答案 0 :(得分:1)

您的代码中存在许多错误/误用,这里应该做什么(问我是否不清楚原因):

require 'spec_helper'

describe Artwork do

  subject(:artwork) { Artwork.new(name:  "foo", ...) }

  it { should respond_to(:name) }

  describe "like should be in effect" do
    let(:liked_user1) { FactoryGirl.create(:user) }
    let(:liked_user2) { FactoryGirl.create(:user) }

    before do
      this_should_raise_an_error
      artwork.save!
    end

    #either        
    it 'some description' do
      expect(artwork.liked_users_count).to eq(0)
    end

    #or
    its(:liked_users_count) { should eq 0 }
  end
end