我在测试中使用rspec和factory_girl。当我打算测试渲染json响应的方法时会出现问题。
我的约会_控制器方法:
def index
@appointments = @user.admin ? @company.appointments : @user.appointments
end
我没有在这里设置格式,因为我只调用这个方法,如“appointmentments.json”。我在我的测试中指定使用:format => :JSON
appointments_controller_spec.rb:
describe "GET index" do
it "assigns all appointments as @appointments" do
appointment = FactoryGirl.create(:appointment)
get :index, { :company_id => user.company.to_param, :user_id => user.to_param }, :format => :json
expect(assigns(:appointments)).to eq(Appointment.all)
end
end
问题是,正如您在堆栈中看到的那样,它仍然会查找html响应:
Failure/Error: get :index, { :company_id => user.company.to_param, :user_id => user.to_param }, :format => :json
ActionView::MissingTemplate:
Missing template appointments/index, application/index with {:locale=>[:es], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.
答案 0 :(得分:0)
尝试添加
respond_with(@appointments)
设置@appointments
后respond_to :json
在控制器的开头。
<强>更新强>
如何指定它:
respond_to do |format|
format.json { render json: @appointments.to_json }
end