1

有人可以帮我弄清楚为什么标记的行#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 确实返回了一些东西 - a Proc.

4

1 回答 1

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

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

于 2012-07-13T04:00:49.660 回答