2014-10-16 2 views
0

Im using rspec и factory_girl на моих тестах. Проблема возникает, когда я намереваюсь протестировать метод рендеринга json response.rspec MissingTemplate error on: format =>: json method

Мой метод appointments_controller:

def index 
    @appointments = @user.admin ? @company.appointments : @user.appointments 
end 

Im не установить формат здесь, потому что я только называю этот метод, как "appointments.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

Попробуйте добавить

respond_with(@appointments) 

после настройки @назначения

respond_to :json 

в начале контроллера.

Update

Как об указании так:

respond_to do |format| 
    format.json { render json: @appointments.to_json } 
end 
+0

Благодаря @sanfor. Теперь это ActionController :: UnknownFormat error – ntonnelier

+0

Still UnknownFormat. В любом случае, я отвечу на ваш ответ, потому что он разрешил ошибку MissingTemplete – ntonnelier

Смежные вопросы