rspec与“should_receive”期望失败

时间:2014-07-24 07:29:43

标签: ruby-on-rails rspec rspec2

下面是错误和针对我的失败测试列出的规范,即使我看到@user.save

上的方法被调用,也不确定为什么它继续失败并显示以下消息
Failure/Error: expect(@performer).should_receive(:store_performer)
       (#<RSpec::Expectations::ExpectationTarget:0x007f9ec42b5808>).store_performer(any args)
           expected: 1 time
           received: 0 times

再一次 如果我发表评论 should_receive 我确实在 @user.save调用中看到了调用方法(通过置证确认)(在此期间我还会检​​查规范内部和调用方法内的@performer的object_id,发现它们是相同的)

注意:基本上store_performerupdate_minimum_payoutupdate_to_be_paid是after_save回调

仅供参考测试用例

it 'should invoke callback method' do
  new_single_performer
  @performer = @user.performer
  expect(@performer).should_receive(:store_performer)
  expect(@performer).should_receive(:update_minimum_payout)
  expect(@performer).should_receive(:update_to_be_paid)
  @user.save
end

User.rb

class User <  ActiveRecord::Base

  # Activity Log (formerly called User Activity)
  cattr_accessor :current_user



  has_one :performer,
    :dependent => :destroy, :validate => true,:inverse_of => :user,:include => :profile # auto load the profile
  ## Some more association

  scope :performers, where(:is_performer => true)
  scope :active, where(:active => true)
  ## Some more scope

  ## Validation
  validates_presence_of :first_name, :last_name, :email #, :password, :password_confirmation
  validates_presence_of :site_id, :unless => :is_referrer?
  validates_format_of :first_name, :message => "Characters not allowed: $\#@!^&*(){}", :with => /^[\w\s]+z/
  validates_format_of :last_name, :message => "Characters not allowed: $\#@!^&*(){}", :with => /^[\w\s]+z/
  validates_format_of :email,  :message => "Invalid Email address", :with => /[\w\.\-\@]+z/
  before_save :geocode, :if => :should_change_geocode?
  before_save :check_role
  after_create :update_vendor_id
  after_create :notify_admin
  after_save  :store_changes,:if => :if_changed?
  after_save :setup_account,:if => :studio_active_changed?
  after_save :approval_notification,:if => :send_notification_present?

Performer.rb

class Performer < ActiveRecord::Base


  belongs_to :user, :conditions => {:is_performer => true},:inverse_of => :performer
  # validations
  validates :date_of_birth, :address_1, :city, :state, :zipcode, :country, :phone_number, 

  after_save :store_performer
  after_destroy :destroy_performer_source
  after_save :update_minimum_payout ,:if => :minimum_payout_changed?
  after_save :update_to_be_paid ,:if => :include_in_payouts_changed?

  private
    def store_performer
      ## Performer Source is Mongo document
      performer_source = PerformerSource.find_or_create_by(:performer_id => self.id)
      performer_source.update_attributes(performer_hash)
      performer_source
    end

    def update_minimum_payout
      self.current_payout.update_attributes(:minimum_payout => self.minimum_payout)
    end

    def update_to_be_paid    
      self.current_payout.update_attributes(:to_be_paid => self.include_in_payouts)
    end
end

PerformerSource

 class PerformerSource
    include Mongoid::Document
   end

JFI Rspec Version是2.12.2

0 个答案:

没有答案