0
ちょっと私はrspecで私のnew
アクションをテストしようとしています。rspecテストにidを渡すレール
私のためのnew
のアクションはscores_controller
のスコアは、サイトに関連付ける予定です。ここでsite
has_many
scores
とscore
belongs_to
site
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
あなたのルートが正しいと仮定すると、まずテストでサイトを作成する必要があります。だからmy_site = Site.create!(site_attribute:attribute_value)。 get:new、id:my_site.id –
'with:site'は既にそうしています – Spance
rakeルートの出力は何ですか?あなたのルーティングが間違っている可能性があります。またあなたの団体を投稿することができます。 –