简体   繁体   English

使用factory_girl进行rspec私有方法测试

[英]rspec private method test with factory_girl

cashout.rb 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 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

These are my model file and test file.The test results is 这些是我的模型文件和测试文件。测试结果是

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)>'

Do you know how can I test private methods ? 您知道如何测试私有方法吗?

您可以通过send调用私有方法:

@cashout3.send(:partner_exist?).should eql(false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM