我有一个简单User
的Score
类和一个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)
当我执行ormodel.model.create(blah)
或 model.model.new(blah)`时如何正确测试?
谢谢你