Ruby on rails 测试控制器方法。Rspec
我想测试控制器方法,但我找不到带有顺序和搜索的测试方法示例。 这是我的控制器:Ruby on rails 测试控制器方法。Rspec,ruby-on-rails,ruby,rspec,rspec-rails,Ruby On Rails,Ruby,Rspec,Rspec Rails,我想测试控制器方法,但我找不到带有顺序和搜索的测试方法示例。 这是我的控制器: class Admin::HotelsController < Admin::BaseController helper_method :sort_column, :sort_direction def index @hotels = Hotel.search(params[:search], params[:search_column]).order(sort_column + ' ' + s
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
我不知道如何测试索引方法。请帮助我或给我一个关于这个信息的链接。谢谢 我建议使用
describe 'GET index' do
let(:hotel1) { create(:hotel) }
let(:hotel2) { create(:hotel) }
it 'render index template' do
get :index
expect(response).to render_template :index
end
it 'render asc ordered hotels' do
get :index
# if you are using json responses
json = JSON.parse(response.body)
expect(json['hotels'].first).to eq hotel1
expect(json['hotels'].last ).to eq hotel2
# or any similar approach to get test the hotels in response
end
it 'render desc ordered hotels' do
get :index, {direction: 'desc'}
# if you are using json responses
json = JSON.parse(response.body)
expect(json['hotels'].first).to eq hotel2
expect(json['hotels'].last ).to eq hotel1
# or any similar approach to get test the hotels in response
end
# you can complete these tests yourself
it 'render hotels sorted with different_column_than_created_at asc'
it 'render hotels sorted with different_column_than_created_at desc'
end
我建议使用
describe 'GET index' do
let(:hotel1) { create(:hotel) }
let(:hotel2) { create(:hotel) }
it 'render index template' do
get :index
expect(response).to render_template :index
end
it 'render asc ordered hotels' do
get :index
# if you are using json responses
json = JSON.parse(response.body)
expect(json['hotels'].first).to eq hotel1
expect(json['hotels'].last ).to eq hotel2
# or any similar approach to get test the hotels in response
end
it 'render desc ordered hotels' do
get :index, {direction: 'desc'}
# if you are using json responses
json = JSON.parse(response.body)
expect(json['hotels'].first).to eq hotel2
expect(json['hotels'].last ).to eq hotel1
# or any similar approach to get test the hotels in response
end
# you can complete these tests yourself
it 'render hotels sorted with different_column_than_created_at asc'
it 'render hotels sorted with different_column_than_created_at desc'
end
如果可能对您有所帮助,出于各种原因,我个人更喜欢对控制器进行最低限度测试: 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
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
然后在模型测试中测试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呈现系统是否正常工作。我假设这不是您想要测试的,您可能更喜欢(我会这么做):
然后使用类似于capybara gem的功能测试测试web页面上应该显示的内容,除非您呈现一些json:在本例中,匹配控制器中的json值
顺便说一下:“@hotel=create(:hotel)”这里不需要@,因为您在“it”中。此外,您还可以创建这样的条目:
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
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!
如果可能对您有所帮助,出于各种原因,我个人更喜欢对控制器进行最低限度测试: 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
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
然后在模型测试中测试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呈现系统是否正常工作。我假设这不是您想要测试的,您可能更喜欢(我会这么做):
然后使用类似于capybara gem的功能测试测试web页面上应该显示的内容,除非您呈现一些json:在本例中,匹配控制器中的json值
顺便说一下:“@hotel=create(:hotel)”这里不需要@,因为您在“it”中。此外,您还可以创建这样的条目:
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
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!
尝试使用
eq
而不是match\u array
。谢谢,它们的不同之处在于,即使是部分匹配,match\u array也会返回true?是的替代形式,它不关心顺序。尝试使用eq
而不是match\u array
。谢谢,它们的不同之处在于,即使是部分匹配,match\u array也会返回true?是的另一种形式,它不关心顺序。谢谢你的回答,我会尽量坚持。谢谢你的回答,我会尽量坚持。谢谢你的回答,或多或少理解。我有一些问题,当我开始这个测试时,他不工作,因为一个JSON文本必须至少包含两个八位字节,但我不知道为什么,因为我确定我的身体有两个以上的八位字节。你能不能作为响应.body
,因为我添加了json部分作为示例来了解这个想法。。。与实际代码不同。。。因此,如果您发布response.body
我可能可以编辑答案如何操作,请告诉我?只需在get:index
之后和@json=……
之前添加p response.body
,然后再次运行测试感谢您的答案,或多或少理解。我有一些问题,当我开始这个测试时,他不工作,因为一个JSON文本必须至少包含两个八位字节,但我不知道为什么,因为我确定我的身体有两个以上的八位字节。你能不能作为响应.body
,因为我添加了json部分作为示例来了解这个想法。。。与实际代码不同。。。因此,如果您发布response.body
我可能可以编辑答案如何操作,请告诉我?只需在get:index
之后和@json=…
之前添加p response.body
,然后再次运行测试