如何检查 RSpec 控制器规范中分配的模型实例变量

发布于 2024-10-07 08:25:17 字数 1485 浏览 1 评论 0原文

我是 RSpec 的新手,但我正在尝试为 Rails 3 应用程序中的一些新功能创建 RSpec2 控制器规范。我有一个属于_to :user 的模型,我想确保当我创建模型的新实例时,用户由我的控制器设置。

当前登录的用户应该是在创建操作中分配给模型的用户。我无法让我的规格正常工作。这是我所拥有的

# this was generated by rspec-rails and sets up my mock model
def mock_plan_order(stubs={})
  (@mock_plan_order ||= mock_model(PlanOrder).as_null_object).tap do |plan_order|
    plan_order.stub(stubs) unless stubs.empty?
  end
end

# here's the actual test that's not working
context "user logged in" do
    before(:each) do
      login_user # this logs in and sets @current_user - this is working
    end

    describe "POST create" do
        # the spec that is not working
        it "sets the user id to the logged on user" do
          PlanOrder.stub(:new).with({"days_per_week" => 3, "description"=>"Test", "purpose_id"=>1 }) { mock_plan_order(:save => true) }
          post :create, :plan_order =>{"days_per_week" => 3, "description"=>"Test", "purpose_id"=>1 }
          assigns(:plan_order).user.should equal(@current_user)
        end
   end
end

这是规范输出

1) PlanOrdersController user logged in POST create with valid params sets the user id to the logged on user
 Failure/Error: assigns(:plan_order).user.should equal(@current_user)

 expected #<User:58754688> => #<User:0x3808680 @name="User_1">
      got #<PlanOrder:58689360> => #<PlanOrder:0x37f8750 @name="PlanOrder_1010">

有人知道我做错了什么吗?

I'm new to RSpec, but I'm attempting to create RSpec2 controller specs for some new functionality in a Rails 3 app. I have a model that belongs_to :user and I want to ensure that the user is getting set by my controller when I create a new instance of my model.

The currently logged on user should be the user that is assigned to the model in the create action. I'm having trouble getting my specs to work. Here's what I have

# this was generated by rspec-rails and sets up my mock model
def mock_plan_order(stubs={})
  (@mock_plan_order ||= mock_model(PlanOrder).as_null_object).tap do |plan_order|
    plan_order.stub(stubs) unless stubs.empty?
  end
end

# here's the actual test that's not working
context "user logged in" do
    before(:each) do
      login_user # this logs in and sets @current_user - this is working
    end

    describe "POST create" do
        # the spec that is not working
        it "sets the user id to the logged on user" do
          PlanOrder.stub(:new).with({"days_per_week" => 3, "description"=>"Test", "purpose_id"=>1 }) { mock_plan_order(:save => true) }
          post :create, :plan_order =>{"days_per_week" => 3, "description"=>"Test", "purpose_id"=>1 }
          assigns(:plan_order).user.should equal(@current_user)
        end
   end
end

Here's the spec output

1) PlanOrdersController user logged in POST create with valid params sets the user id to the logged on user
 Failure/Error: assigns(:plan_order).user.should equal(@current_user)

 expected #<User:58754688> => #<User:0x3808680 @name="User_1">
      got #<PlanOrder:58689360> => #<PlanOrder:0x37f8750 @name="PlanOrder_1010">

Anyone know what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您对 plan_order 对象的创建进行了存根,因此不会设置任何用户。要么不模拟/存根,添加模拟以确保设置用户。

You stubbed the creation of the plan_order object, so no user will be set. Either don't mock/stub, add a mock to ensure that the user is being set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文