[英]Factory_girl and RSpec: undefined method `new' for Test:Module
[英]rspec private method test with factory_girl
cashout.rb
class Cashout < ActiveRecord::Base
belongs_to :partner
private
def partner_exist?
if self.partner.nil?
errors.add(:base, "There is no partner! ")
return false;
end
return true
end
end
cashout_spec.rb
context 'should check partner existence' do
it 'if partner is not nil' do
@company = Factory(:company)
@partner = Factory(:partner, :company => @company)
@cashout = Factory.build(:cashout, :partner => @partner)
@cashout.save
@cashout.partner_exist?.should eql(true)
end
end
這些是我的模型文件和測試文件。測試結果是
1) Cashout should check partner existence if partner is nil
Failure/Error: @cashout3.partner_exist?.should eql(false)
NoMethodError:
private method `partner_exist?' called for #<Cashout:0x007f822189dfa0>
# ./spec/models/cashout_spec.rb:47:in `block (3 levels) in <top (required)>'
您知道如何測試私有方法嗎?
您可以通過send
調用私有方法:
@cashout3.send(:partner_exist?).should eql(false)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.