0

提现.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)>'

你知道我如何测试私有方法吗?

4

1 回答 1

4

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

@cashout3.send(:partner_exist?).should eql(false)
于 2013-04-16T00:02:45.443 回答