vendredi 26 février 2016

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

Aucun commentaire:

Enregistrer un commentaire