gpt4 book ai didi

ruby-on-rails - 测试 Controller 方法。规范

转载 作者:太空宇宙 更新时间:2023-11-03 18:16:34 25 4
gpt4 key购买 nike

我想测试controller的方法,但是找不到用order和search来测试方法的例子。 这是我的 Controller :

class Admin::HotelsController < Admin::BaseController
helper_method :sort_column, :sort_direction
def index
@hotels = Hotel.search(params[:search], params[:search_column]).order(sort_column + ' ' + sort_direction)
end

def show
@hotel = Hotel.find(params[:id])

end

def update
@hotel = Hotel.find(params[:id])
if @hotel.update_attributes(hotel_params)
redirect_to admin_hotels_path
else
render(:edit)
end
end

private
def hotel_params
params.require(:hotel).permit(:title, :description, :user_id, :avatar, :price, :breakfast, :status, address_attributes: [:state, :country, :city, :street])
end

def sort_column
Hotel.column_names.include?(params[:sort]) ? params[:sort] : 'created_at'
end

def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc'
end
end

这是对该 Controller 的测试。

require 'rails_helper'

describe Admin::HotelsController do
login_admin
describe 'GET index' do
it 'render a list of hotels' do
hotel1, hotel2 = create(:hotel), create(:hotel)
get :index
expect(assigns(:hotels)).to match_array([hotel1, hotel2])
end
end

describe 'GET show' do
it 'should show hotel' do
@hotel = create(:hotel)
get :show, { id: @hotel.to_param, template: 'hotels/show' }
expect(response).to render_template :show
end
end


end

我不知道如何测试索引方法。请帮助或给我一个有关此信息的链接。谢谢!

最佳答案

如果对您有帮助,出于各种原因,我个人更喜欢对 Controller 进行最少的测试:

1) 当我开始进行 Rails 测试时,我读了很多文章说这是个好主意2) 它允许您测试隔离模型方法:

describe 'GET index' do
it 'render a list of hotels' do
hotel1, hotel2 = create(:hotel), create(:hotel)
get :index
expect(assigns(:hotels)).to match_array([hotel1, hotel2])
end
end

此处您的测试与您对模型的查询结果相匹配。你可以这样拆分它:

describe 'GET index' do
it 'render a list of hotels' do
hotel1, hotel2 = create(:hotel), create(:hotel)
Hotel.should_receive(:search).with(YOUR PARAMS)
get :index
response.response_code.should == 200
end
end

然后在模型测试中测试Hotel.search的结果。

3) 它允许您测试功能,而不是一些不相关的随机事物:

describe 'GET show' do
it 'should show hotel' do
@hotel = create(:hotel)
get :show, { id: @hotel.to_param, template: 'hotels/show' }
expect(response).to render_template :show
end
end

此处“expect(response).to render_template :show”似乎是在测试 Rails 渲染系统是否正常工作。我假设这不是你想要测试的,你可能更喜欢(这就是我会做的):

describe 'GET show' do
it 'should show hotel' do
@hotel = create(:hotel)
Hotel.should_receive(:find).with(YOUR PARAMS)
get :show, { id: @hotel.to_param, template: 'hotels/show' }
response.response_code.should == 200
end
end

然后使用类似 capybara gem 的功能测试来测试应该出现在网页上的内容,除非您正在渲染一些 json:在这种情况下匹配 Controller 中的 json 值。

顺便说一句:“@hotel = create(:hotel)” @ 在这里不是必需的,因为您在“它”中。此外,您可以像这样创建这样的条目:

context "" do
before(:each) do
@hotel = create(:hotel) # here the @ is necessary for the variable to be
end # accessible in the it
it "" do
end
end

甚至像这样:

context "" do
let(:hotel) { create(:hotel) } # you can call it in the test by using hotel and it
it "" do # will be insert in you db only when it's in the "it"
end # if you want it to be created in the "it" without
end # calling hotel for nothing, use let!

关于ruby-on-rails - 测试 Controller 方法。规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25188753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com