简体   繁体   English

测试Rails中的嵌套路线

[英]Testing nested routes in rails

I was writing integrations tests for my application. 我正在为我的应用程序编写集成测试。 I am familiar with the controller tests and the normal integration testing. 我熟悉控制器测试和常规集成测试。 However, I am not sure how to test nested resources in the integration testing. 但是,我不确定如何在集成测试中测试嵌套资源。 I have a teams model which is nested inside the scoreboards model. 我有一个嵌入在计分板模型中的团队模型。 The relevant routes code is given below. 相关路线代码如下。

resources :scoreboards do 
   resources :teams, only: [:edit, :create, :destroy, :update]
 end

I want to test all the important workflows for teams. 我想测试团队的所有重要工作流程。 For example, the invalid and valid creation of teams. 例如,无效和有效的团队创建。 The test code is written below. 测试代码如下所示。

def setup
    ActionMailer::Base.deliveries.clear
    @scoreboard = scoreboards(:scoreboard_a)
  end



test 'Invalid creation of the teams' do
    assert_no_difference 'Team.count' do
      post scoreboards_teams_path(@scoreboard), team: {name: " ", 
                                                        win: 0, 
                                                       loss: 0, 
                                                        tie: 0 }

    end
  end 

I have the validation set up in such a way that team name must be present. 我以必须提供团队名称的方式进行了验证。 The problem is with the routes. 问题在于路线。 I also have an association set up with scoreboard_a. 我也与scoreboard_a建立了关联。 The teams.yml file is given below. 在下面给出了teams.yml文件。

team_a:
    name: team
    win: 1
    loss: 2
    tie: 0
    id: 2
    scoreboard: scoreboard_a 

I get a no method error. 我收到无方法错误。 The error is given below. 错误在下面给出。

`NoMethodError: undefined method `scoreboards_teams_path'. 

Since teams are nested inside scoreboards show page. 由于团队嵌套在记分板显示页面内。 Therefore, there isn't a new action I can call the get request to. 因此,没有可以调用get请求的新操作。 My question is, how would I call a post request to the teams object. 我的问题是,我将如何向团队对象发出发帖请求。 I am not exactly sure how to do that. 我不确定该怎么做。 I have tried looking through documentation but there isn' really anything on nested routes. 我尝试浏览文档,但嵌套路线上确实没有任何内容。 I have other objects that are nested inside scoreboards as well. 我还有嵌套在计分板上的其他对象。 Therefore, understanding how nested routes are tested in rails would really go a long way. 因此,了解如何在铁轨中测试嵌套路线将大有帮助。 As always, any help is greatly appreciated. 与往常一样,我们非常感谢您的帮助。 Thanks!! 谢谢!!

Rails guides has a section on nested helper resources that should sufficiently answer your question: http://guides.rubyonrails.org/routing.html#nested-resources Rails指南中有关于嵌套帮助程序资源的部分,该部分应足以回答您的问题: http : //guides.rubyonrails.org/routing.html#nested-resources

You can call a new action with the named helper: 您可以使用命名的帮助程序调用新操作:

new_scoreboard_team_path

The post named helper for you would be 名为helper的帖子将是

scoreboard_teams_path

your code says scoreboards_teams_path 您的代码显示scoreboards_teams_path

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

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