1
請幫忙解決這個問題。我需要爲索引操作測試渲染模板。如何用嵌套路線測試控制器?
routes.rb中:
resources :users do
resources :albums
end
耙路線:
user_albums GET /users/:user_id/albums(.:format) albums#index
相冊控制器:
class AlbumsController < ApplicationController
before_action :set_album, only: [:show, :edit, :update, :destroy, :get_album_data]
def index
@user = User.find(params[:user_id])
@albums = @user.albums.paginate(page: params[:page], :per_page => 6).order(created_at: :DESC)
@album = Album.new
end
.........
.........
private
def set_album
@album = Album.find(params[:id])
end
def album_params
params.require(:album).permit(:title, :description)
end
end
albums_controller_spec.rb:
require 'spec_helper'
describe AlbumsController do
describe 'index action' do
it 'render index template if user is found' do
user = FactoryGirl.create(:user)
get :index, { user_id: user.id }
response.should render_template('index')
end
end
end
個
工廠:
FactoryGirl.define do
factory :user do
name 'qqq'
sequence(:email){ |i| "use#{i}@ad.ad" }
password 'qwerty'
password_confirmation{ |u| u.password }
end
end
我在控制檯中運行它:
rake db:reset RAILS_ENV=test
rspec spec
控制檯顯示以下錯誤信息:
F.
Failures:
1) AlbumsController index action render index template if user is found
Failure/Error: get :index, { user_id: user.id }
NoMethodError:
undefined method `get' for #<RSpec::ExampleGroups::AlbumsController::IndexAction:0x000000067fc668>
# ./spec/controllers/albums_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.13261 seconds (files took 1.91 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/controllers/albums_controller_spec.rb:5 # AlbumsController index action render index template if user is found
請幫我 '指數' 測試動作
你的'albums_controller_spec.rb'缺少'require'rails-helper''嗎? – max
('spec-helper')舊版本的RSpec-rails – max
它在我的文件中。我只是沒有 – stackov8