Rails 3 使用 Rspec、嵌套资源和继承资源获得错误的模拟模型

发布于 2024-11-17 06:08:37 字数 1889 浏览 3 评论 0原文

我正在尝试使用 Rspec 和模拟模型测试嵌套控制器,但出现这些错误:

Failure/Error: it { assigns[:store].should equal(mock_store) } 
   expected #<Store:2198414660> => #<Store:0x83092544 @name="Store_1008">
        got #<User:2198383140> => #<User:0x8308aa24 @name="User_1009">

并且我找不到发生这种情况的原因。

routes.rb

namespace :api do
  namespace :v1 do
    devise_for :users, :controllers => { :sessions => "devise/sessions", 
                                         :passwords => "devise/passwords", 
                                         :registrations => "devise/registrations" }
    resources :categories, :only => [ :index, :show ]
    resources :users, :only => [ :index, :show ] do
      resources :stores
    end

    resources :stores do
      resources :products
    end

    resources :products
  end
end

app/controllers/api/v1/stores_controller.rb

class Api::V1::StoresController < Api::ApiController
  belongs_to :user
  actions :all
end

app/controllers/api/api_controller.rb

class Api::ApiController < InheritedResources::Base
  respond_to :json
end

我的spec/controllers/api/v1/stores_controller_spec.rb -> http://www.pastie.org/2118818 向我抛出这些奇怪的错误 -> http://www.pastie.org/2118825

为什么我总是得到被嘲笑的用户而不是商店。

如果我从app/controllers/api/v1/stores_controller.rb中删除belongs_to :user行,我的所有测试都会通过,但我有兴趣测试“users/:user_id/stores/*”路由。

我如何正确测试它们,以及为什么我得到一个模拟用户而不是商店。

我正在使用

* inherited_resources (1.2.2)
* rails (3.0.7)
* rspec (2.5.0)
* rspec-core (2.5.2)
* rspec-expectations (2.5.0)
* rspec-mocks (2.5.0)
* rspec-rails (2.5.0)

感谢您的时间。

I'm trying to test a nested controller with Rspec and mocked models but i'm getting these errors:

Failure/Error: it { assigns[:store].should equal(mock_store) } 
   expected #<Store:2198414660> => #<Store:0x83092544 @name="Store_1008">
        got #<User:2198383140> => #<User:0x8308aa24 @name="User_1009">

and i can't find why this is happening.

routes.rb

namespace :api do
  namespace :v1 do
    devise_for :users, :controllers => { :sessions => "devise/sessions", 
                                         :passwords => "devise/passwords", 
                                         :registrations => "devise/registrations" }
    resources :categories, :only => [ :index, :show ]
    resources :users, :only => [ :index, :show ] do
      resources :stores
    end

    resources :stores do
      resources :products
    end

    resources :products
  end
end

app/controllers/api/v1/stores_controller.rb

class Api::V1::StoresController < Api::ApiController
  belongs_to :user
  actions :all
end

app/controllers/api/api_controller.rb

class Api::ApiController < InheritedResources::Base
  respond_to :json
end

My spec/controllers/api/v1/stores_controller_spec.rb -> http://www.pastie.org/2118818 throws me these strange errors -> http://www.pastie.org/2118825.

Why do i always get the mocked User instead the Store.

If I remove the belongs_to :user line from app/controllers/api/v1/stores_controller.rb all my test pass, but i'm interested in testing "users/:user_id/stores/*" routes.

How can i test them correctly, and why i'm getting a Mocked User instead the Store.

I'm using

* inherited_resources (1.2.2)
* rails (3.0.7)
* rspec (2.5.0)
* rspec-core (2.5.2)
* rspec-expectations (2.5.0)
* rspec-mocks (2.5.0)
* rspec-rails (2.5.0)

Thanks for your time.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文