我在我的模型中测试一个before_create回调是死路一条。
我的规格
# The spec
let(:stamp){ mock_model(CompanyStamp) }
let(:signature){ mock_model(CompanyHandwrittenSignature) }
let(:account) { mock_model(Account, :company_handwritten_signature => signature, :company_stamp => stamp) }
it "should have signature if the account has signature" do
subject.stub :account => account
subject.stub :save => true
subject.company_handwritten_signature.should == signature
end
这是我的模型代码
# the model's code
before_create do |element|
puts "element.account ===> #{element.account.inspect}" # element has no account! wtf!?
element.company_handwritten_signature ||= element.account.company_handwritten_signature
element.company_stamp ||= element.account.company_stamp
true
end
正在调用before_filter方法,但在该回调中,帐户方法的存根似乎不起作用。 我已经测试了
subject.account = account,结果相同。发生了什么事?
顺便说一下,我使用的是rails 2.3和rspec-rails 1.3.4
答案 0 :(得分:0)
你对方法进行存根的方式看起来不正确。我总是使用以下内容:
subject.stub(:method).and_return(value)
你试过这种格式吗?另外,你为什么要存钱?这似乎可能会影响一些预期的ActiveRecord行为。
let(:stamp){ mock_model(CompanyStamp) }
let(:signature){ mock_model(CompanyHandwrittenSignature) }
let(:account) { mock_model(Account, :company_handwritten_signature => signature, :company_stamp => stamp) }
it "should have signature if the account has signature" do
subject.stub(:account).and_return(account)
subject.stub(:save).and_return(true)
subject.company_handwritten_signature.should == signature
end