2016-05-03 16 views
0

ちょっと私はrspecで私のnewアクションをテストしようとしています。rspecテストにidを渡すレール

私のためのnewのアクションはscores_controllerのスコアは、サイトに関連付ける予定です。ここでsitehas_manyscoresscorebelongs_tosite

def new 
    @score = Score.new(site: Site.find(params['site_id'])) 
    end 

た瞬間に私のRSpecのテストです:

describe 'new' do 
    with :user 
    with :score 
    with :site 
    before do 
     sign_in user 
     get :new, id: site.id 
    end 
    it { expect(response).to have_http_status :ok } 
    it { should render_template :new } 
    it 'assigns a new score as @score' do 
     expect(assigns(:score)).to be_a_new(Score) 
    end 
    end 

そして、ここでは、私が

1) ScoresController new 
    Failure/Error: @score = Score.new(site: Site.find(params['site_id'])) 

    ActiveRecord::RecordNotFound: 
     Couldn't find Site with 'id'= 
    # ./app/controllers/scores_controller.rb:19:in `new' 
    # ./spec/controllers/scores_controller_spec.rb:41:in `block (3 levels) in <top (required)>' 
    # ./spec/spec_helper.rb:31:in `block (3 levels) in <top (required)>' 
    # ./spec/spec_helper.rb:30:in `block (2 levels) in <top (required)>' 

を得ているエラーですid: site.idが間違っていることを知っていますが、私はでテストする方法を理解できません私はまたレールには本当に新しいので、それは非常に明らかな場合はごめんなさい。ここにある私のルート

    Prefix Verb URI Pattern     Controller#Action 
        root GET /       sites#index 
        sites GET /sites(.:format)    sites#index 
         POST /sites(.:format)    sites#create 
       new_site GET /sites/new(.:format)   sites#new 
       edit_site GET /sites/:id/edit(.:format)  sites#edit 
        site GET /sites/:id(.:format)   sites#show 
         PATCH /sites/:id(.:format)   sites#update 
         PUT /sites/:id(.:format)   sites#update 
         DELETE /sites/:id(.:format)   sites#destroy 
        scores GET /scores(.:format)    scores#index 
         POST /scores(.:format)    scores#create 
       new_score GET /scores/new(.:format)   scores#new 
       edit_score GET /scores/:id/edit(.:format)  scores#edit 
        score GET /scores/:id(.:format)   scores#show 
         PATCH /scores/:id(.:format)   scores#update 
         PUT /scores/:id(.:format)   scores#update 
         DELETE /scores/:id(.:format)   scores#destroy 
     new_user_session GET /users/sign_in(.:format)  devise/sessions#new 
      user_session POST /users/sign_in(.:format)  devise/sessions#create 
    destroy_user_session DELETE /users/sign_out(.:format)  devise/sessions#destroy 
      user_password POST /users/password(.:format)  devise/passwords#create 
     new_user_password GET /users/password/new(.:format) devise/passwords#new 
     edit_user_password GET /users/password/edit(.:format) devise/passwords#edit 
         PATCH /users/password(.:format)  devise/passwords#update 
         PUT /users/password(.:format)  devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)  devise/registrations#cancel 
     user_registration POST /users(.:format)    devise/registrations#create 
    new_user_registration GET /users/sign_up(.:format)  devise/registrations#new 
    edit_user_registration GET /users/edit(.:format)   devise/registrations#edit 
         PATCH /users(.:format)    devise/registrations#update 
         PUT /users(.:format)    devise/registrations#update 
         DELETE /users(.:format)    devise/registrations#destroy 
+0

あなたのルートが正しいと仮定すると、まずテストでサイトを作成する必要があります。だからmy_site = Site.create!(site_attribute:attribute_value)。 get:new、id:my_site.id –

+0

'with:site'は既にそうしています – Spance

+0

rakeルートの出力は何ですか?あなたのルーティングが間違っている可能性があります。またあなたの団体を投稿することができます。 –

答えて

3

ただ、この行を変更

get :new, id: site.id 

get :new, site_id: site.id 
に:それは

create_table "scores", force: :cascade do |t| 
    t.integer "value",  null: false 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.integer "site_id" 
    t.string "response" 
    end 

    add_index "scores", ["site_id"], name: "index_scores_on_site_id", using: :btree 

    create_table "sites", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

編集必要ならここで

は私のスキーマです

newメソッドではSiteparams[:site_id]でロードするためです。

関連する問題