Johnny Cash
Johnny Cash

Reputation: 5147

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

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 ?

Upvotes: 0

Views: 1150

Answers (1)

Mori
Mori

Reputation: 27779

You can call private methods via send:

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

Upvotes: 4

Related Questions