0

私は単純なクラスUserScoreクラスを持っていますIndexController

class User < ActiveRecord::Base
  has_many :scores
  attr_accessible :scores_attributes, :scores
  accepts_nested_attributes_for :scores
end

class Score < ActiveRecord::Base
  attr_accessible :time_elapsed
  belongs_to :user
end

私のIndexController(簡略化)

def setHighscore
  existing_user = User.find_by_random_token(session[:user][:random_token])
  existing_user.scores.new(time_elapsed: params[:time_elapsed])
  existing_user.save
end

そして私のスペック

describe IndexController do
  it "appends a highscore to an existing user" do
    user = User.create!(valid_session[:user])
    existing_user = User.should_receive(:find_by_random_token).with(random_token).and_return(user)

     existing_user.scores.should_receive(:new).with(time_elapsed: 400)

     post :setHighscore, valid_params, valid_session
end

このエラーが発生しました

RSpec::Mocks::MockExpectationError: (#<RSpec::Mocks::MessageExpectation:0x007fb4fa2b67c0>).new({:time_elapsed=>400})
    expected: 1 time with arguments: ({:time_elapsed=>400})
    received: 0 times with arguments: ({:time_elapsed=>400})

update_attribute(blah)or model.model.create(blah)or model.model.new(blah)`を実行するときに正しくテストするにはどうすればよいですか?

ありがとうございました

4

2 に答える 2