RSpec`undefined method` ..`for proc`在一个非常简单的规范中

时间:2012-07-13 03:50:58

标签: ruby-on-rails rspec

有人可以帮我找出标记为#works的行有效的原因,但标有#causes error的行会导致错误吗? (这是带有rspec-rails 2.11.0的Rails 3.2.6)

错误:undefined method 'more_things=' for #<Proc:0x007fbd23d0b960> (NoMethodError)

规格:

describe User do

  subject { User.new }

  it "has no something" do
    User.new.more_things = "#works"        #works
    subject.something.should be_empty      #works
  end

  describe "#more_things" do
    User.new.more_things = "#works"        #works
    subject.more_things = "#causes error"  #causes error
  end

end

模特:

class User

  def something
    []
  end

  def more_things=(value)

  end

end

我尝试在subject { User.new }内放置另一个describe "#more_things" do,认为这可能是某种范围问题,但这没有帮助(因为subject 不会导致范围问题返回一些内容 - Proc

1 个答案:

答案 0 :(得分:0)

卫生署。慢我。它在it块(或before / after块中)时有效:

describe "#more_things" do
  User.new.more_things = "#works"        #works
  subject.more_things = "#causes error"  #causes error
  it "should work now" do
    subject.more_things = "#works now"   #works
  end
end

我想我应该永远记住它是主题:)