rspec 模拟某些对象时出现问题
当我尝试删除某些方法时,我看到一些奇怪的行为。我正在使用 Rails 3.0.3 和 rspec 2.3.0
这是规范文件的相关部分
require 'spec_helper'
include Authlogic::TestCase
describe PrizesController do
before do
activate_authlogic
@manager = Factory.create(:valid_manager, :name => "Test Manager ")
UserSession.create @manager
end
def mock_prize(stubs={})
(@mock_prize ||= mock_model(Prize, :point_cost => 100).as_null_object).tap do |prize|
prize.stub(stubs) unless stubs.empty?
end
end
def mock_consumable(stubs={})
(@mock_consumable ||= mock_model(Consumable).as_null_object).tap do |consumable|
consumable.stub(stubs) unless stubs.empty?
end
end
describe "GET buy_this" do
it "assigns the requested prize as @prize and requested consumable as @consumable if the player has enough points" do
Prize.stub(:find).with("37") { mock_prize }
@manager.should_receive(:available_points).and_return(1000)
get :buy_this, :id => "37", :user_id => @manager.id
assigns(:prize).point_cost.should eq(100)
assigns(:prize).should be(mock_prize)
assigns(:consumable).should_not be_nil
end
it "assigns the requested prize as @prize and no consumable as @consumable if the player does not have enough points" do
Prize.stub(:find).with("37") { mock_prize }
@manager.should_receive(:available_points).and_return(10)
get :buy_this, :id => "37", :user_id => @manager.id
assigns(:prize).point_cost.should eq(100)
assigns(:prize).should be(mock_prize)
assigns(:consumable).should be_nil
end
end
和控制器方法:
def buy_this
@prize = Prize.find(params[:id])
user = User.find(params[:user_id]) if params[:user_id]
user ||= current_user
flash[:notice] = ("Attempting to redeem points for a prize")
if user.available_points > @prize.point_cost
@consumable = user.consumables.create(:kind => @prize.consumable_kind, :description => @prize.consumable_description, :redemption_message => @prize.consumable_redemption_message)
point_record = @consumable.create_point_record(:redeemed_points => @prize.point_cost)
point_record.user = user
point_record.save
flash[:success] = "You successfully redeemed #{@prize.point_cost} points for #{@prize.name}"
else
flash[:error] = "Sorry, you don't seem to have enough points to buy this"
end
redirect_to prizes_path
end
测试失败,这是输出...
1) PrizesController GET buy_this assigns the requested prize as @prize and requested consumable as @consumable if the player has enough points
Failure/Error: assigns(:consumable).should_not be_nil
expected not nil, got nil
# ./spec/controllers/prizes_controller_spec.rb:39
2) PrizesController GET buy_this assigns the requested prize as @prize and no consumable as @consumable if the player does not have enough points
Failure/Error: @manager.should_receive(:available_points).and_return(10)
(#<User:0x10706b000>).available_points(any args)
expected: 1 time
received: 0 times
# ./spec/controllers/prizes_controller_spec.rb:44
对此有什么想法吗?我完全困惑为什么使用相同参数调用相同方法的两个测试会以不同的方式失败(更不用说,我根本不明白为什么它们会失败......)。
I'm seeing some strange behavior when I'm trying to stub out some methods. I'm using rails 3.0.3 and rspec 2.3.0
Here is the relevant section of the spec file
require 'spec_helper'
include Authlogic::TestCase
describe PrizesController do
before do
activate_authlogic
@manager = Factory.create(:valid_manager, :name => "Test Manager ")
UserSession.create @manager
end
def mock_prize(stubs={})
(@mock_prize ||= mock_model(Prize, :point_cost => 100).as_null_object).tap do |prize|
prize.stub(stubs) unless stubs.empty?
end
end
def mock_consumable(stubs={})
(@mock_consumable ||= mock_model(Consumable).as_null_object).tap do |consumable|
consumable.stub(stubs) unless stubs.empty?
end
end
describe "GET buy_this" do
it "assigns the requested prize as @prize and requested consumable as @consumable if the player has enough points" do
Prize.stub(:find).with("37") { mock_prize }
@manager.should_receive(:available_points).and_return(1000)
get :buy_this, :id => "37", :user_id => @manager.id
assigns(:prize).point_cost.should eq(100)
assigns(:prize).should be(mock_prize)
assigns(:consumable).should_not be_nil
end
it "assigns the requested prize as @prize and no consumable as @consumable if the player does not have enough points" do
Prize.stub(:find).with("37") { mock_prize }
@manager.should_receive(:available_points).and_return(10)
get :buy_this, :id => "37", :user_id => @manager.id
assigns(:prize).point_cost.should eq(100)
assigns(:prize).should be(mock_prize)
assigns(:consumable).should be_nil
end
end
And the controller method:
def buy_this
@prize = Prize.find(params[:id])
user = User.find(params[:user_id]) if params[:user_id]
user ||= current_user
flash[:notice] = ("Attempting to redeem points for a prize")
if user.available_points > @prize.point_cost
@consumable = user.consumables.create(:kind => @prize.consumable_kind, :description => @prize.consumable_description, :redemption_message => @prize.consumable_redemption_message)
point_record = @consumable.create_point_record(:redeemed_points => @prize.point_cost)
point_record.user = user
point_record.save
flash[:success] = "You successfully redeemed #{@prize.point_cost} points for #{@prize.name}"
else
flash[:error] = "Sorry, you don't seem to have enough points to buy this"
end
redirect_to prizes_path
end
The tests fail and this is the output...
1) PrizesController GET buy_this assigns the requested prize as @prize and requested consumable as @consumable if the player has enough points
Failure/Error: assigns(:consumable).should_not be_nil
expected not nil, got nil
# ./spec/controllers/prizes_controller_spec.rb:39
2) PrizesController GET buy_this assigns the requested prize as @prize and no consumable as @consumable if the player does not have enough points
Failure/Error: @manager.should_receive(:available_points).and_return(10)
(#<User:0x10706b000>).available_points(any args)
expected: 1 time
received: 0 times
# ./spec/controllers/prizes_controller_spec.rb:44
Any ideas about this? I'm totally stumped why the two tests calling the same method with the same parameters would fail in different ways (not to mention, I don't understand why they are failing at all...).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论