简体   繁体   English

为什么Rspec使用错误的对象进行控制器测试?

[英]Why is Rspec using the wrong object for a controller test?

We have an API which we returns some structured JSON data. 我们有一个API,可以返回一些结构化的JSON数据。 Sites have_many :controllers, and Controllers belong_to :site 站点have_many:controllers,而Controller属属to_site:site

For the test, we have to create a mock site and controller, which is achieved in all our other feature test files exactly like I have it listed below in the before(:each) do block. 对于测试,我们必须创建一个模拟站点和控制器,这在所有其他功能测试文件中都可以实现,就像我在before(:each)do块中下面列出的一样。

Test: 测试:

describe Api::V2::SitesController, :type => :controller do
    render_views

    before(:each) do
        basic_auth_and_skip_hmac
        @site = FactoryGirl.create(:site)
        @user_site = FactoryGirl.create(:user_site, user: @user, site: @site)
        @controller = FactoryGirl.create(:controller, site: @site)
    end

    it 'List all sites' do
        get :index, format: :json
        puts response.body
        expect(response.body).to include("Site 1")
        expect(response.body).to include("Controller 1")
    end


end

But the response for this controller test is unexpected: 但是此控制器测试的响应是意外的:

Api::V2::SitesController
  List all sites (FAILED - 1)

Failures:

  1) Api::V2::SitesController List all sites
     Failure/Error: get :index, format: :json
     NoMethodError:
       undefined method `response_body=' for #<Controller:0x0000010db0c2d8>

Why do you even care about response_body for the Controller object Rspec? 您为什么还要关心Controller对象Rspec的response_body? It clearly states at the top that we're describing the SitesController! 它清楚地在最上面指出我们正在描述SitesController!

Removing the creation of the controller object and the matching expectation at the bottom of the file makes the test pass as expected: 除去控制器对象的创建以及文件底部的匹配期望,测试将按预期通过:

Finished in 0.60435 seconds (files took 5.38 seconds to load)
1 example, 0 failures

But I'm not really testing everything I set out to test because my JSON includes: 但是我并没有真正测试我打算测试的所有内容,因为我的JSON包括:

"controllers":[]

Which technically cannot happen in our application. 从技术上讲,这在我们的应用程序中不会发生。 The controller is the most important unit to measure for us, so returning a JSON response with valid site information but no controllers would be pointless. 控制器是衡量我们最重要的单位,因此返回带有有效站点信息但没有控制器的JSON响应将毫无意义。

As shown in the discussion above with Mike - it turns out that "@controller" is special to Ruby. 如上面与Mike的讨论中所示-事实证明,“ @ controller”是Ruby的特殊功能。

And I happen to work in probably the only industry where this becomes a naming conflict. 而且我恰好在可能成为命名冲突的唯一行业工作。 We manage a service for irrigation controllers, so the word is always messing with my head - am I talking about MVC controller or the actual device? 我们为灌溉控制器管理一项服务,所以这个词总是让我感到困惑-我是在谈论MVC控制器还是实际设备?

It's been a burden that probably no one else will ever encounter as it's just not a variable you would ever think to use. 这是一个负担,可能没有其他人会遇到,因为这不是您想使用的变量。

In summary - don't ever call @controller, pretty much anywhere. 总结-永远不要在任何地方调用@controller。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM