一個rspec控制器測試的小例子。你可能會從這裏得到一些想法。
#在這裏定義你的幫手。 要求'rails_helper'
# respect method to define test for your controller.
RSpec.describe YourController, type: :controller do
#define your factories for the set of your test
# you will decide which one you need to create from factory. Factory girl
# is used to mock factories.
# @c = params.has_key?(:car) ? Car.new(car_listing_params) : Car.new()
# @car.user = @user
# @car.number_of_beds = 4 unless @car.number_of_beds
# @car.car_type_tag = 'car' unless @car.car_type
# @page_title = t('home.list_your_car')
let(:user) { FactoryGirl.create :user }
before { sign_in user }
describe '#title' do
context 'with valid params' do
before do
# in before block, you can request a http call
# with parameter. This action repeats before each test runs.
get :your_action_of_current_controller, {a_param: a_value, a_date: "16-4-2015"}
end
it "renders successfully" do
# when you call a get request it assigns a response object.
expect(response).to be_success
end
it "returns json" do
expect(response.content_type).to be == 'application/json'
end
end
end
end
它需要過於寬泛的答案,雖然我可以給你一個小測試的例子。 – Rubyrider
任何你認爲會使我處於正確測試方向的東西會有所幫助。謝謝。 – user2645029
是檢查我的答案。這可能非常複雜。我只是想給你一個評論提示。如果您沒有得到任何意見,請隨時發表評論。 – Rubyrider