[rspec-users] How to test update_attribute with mocha?

anmaxp andresmax at gmail.com
Wed May 13 18:32:41 EDT 2009


Hey everyone,

I'm having sort of an issue here, I'm trying to test the update of the
last_login field upon login, here is my test

  it "assigns a new last_login timestamp" do
    User.expects(:update_attribute).at_least_once.returns(true)
    post :create, :email => 'bob at example.com', :password => 'test'
 end

sessions_controller:

  def create

    self.current_user = current_account.users.authenticatex(params
[:email], params[:password], current_account)

    if logged_in?

      Time.zone = current_user.timezone
      I18n.locale = current_user.language

      login_flash(current_user)
      current_user.login_updates(current_account.id)

      redirect_back_or_default(default_path)

    else
      flash.now[:error] = t('flash.notice.invalidcredentials')
      render :action => 'new'
    end
  end

and the login updates_method on the user model:

  def login_updates(account)
    self.update_attribute(:last_login, Time.zone.now)
  end

no matter what variation of expects(:update), expects
(:update_attribute) I always get the same failed test:

#<Mock:0x53091e8>.update_attribute(any_parameters) - expected calls:
at least 1, actual calls: 0


More information about the rspec-users mailing list