简体   繁体   中英

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. The teams.yml file is given below.

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. 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

You can call a new action with the named helper:

new_scoreboard_team_path

The post named helper for you would be

scoreboard_teams_path

your code says scoreboards_teams_path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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