测试控制器方法。 Rspec的

时间:2014-08-07 17:36:49

标签: 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 + ' ' + 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 

这是对此控制器的测试。

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

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

2 个答案:

答案 0 :(得分:2)

如果它对您有所帮助,我个人更喜欢对控制器进行最小化测试,原因有多种:

1)当我开始进行测试时,我读了许多文章说这是一个好主意 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

这里“期待(响应).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:在这种情况下匹配控制器中的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!

答案 1 :(得分:1)

我建议使用

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