0

我在 Rails 2-3-stable 上使用 factory_girl + rspec:

测试:

   context "test" do
      before(:each) do
        @profile.stub!(:lastfm_enabled?).and_return(true)
      end
      it "should be able to scrobble if true" do
        @profile.update_attribute(:lastfm_scrobbling, true)
        @profile.lastfm_scrobbling_enabled?.should be_true
      end
    end

代码:

def lastfm_scrobbling_enabled?
  lastfm_enabled? && lastfm_scrobbling
end

这个简单的测试失败了,加上真正的lastfm_enabled?方法被真正调用。

@profile 不是模拟,它是用 factory_girl 创建的。

最初我也对 lastfm_scrobbling 属性进行了 stub,但我正在考虑 factory_girl 的工作方式,因此更喜欢直接设置它。

知道为什么吗?

4

1 回答 1

3

您需要存根 read_attribute 方法

  before(:each) do
    @profile.stub!(:read_attribute).with(:lastfm_enabled).and_return(true)
  end
于 2010-10-04T15:57:32.967 回答