2016-01-26 3 views
0

Я использую жонжирование jsonapi-serializers, и у меня возникли проблемы с выяснением того, как тестировать почтовый запрос с помощью rspec и полезной нагрузки json. Я знаю, что это работает, потому что я могу использовать почтальон и отправлять json, и он успешно создает новый объект, но я не уверен, почему я не могу заставить rspec test работать.Тестирование сообщения rspec с Rails JSONAPI :: Сериализатор

Вот метод контроллера апи:

def create 
    @sections = @survey.sections.all 
    if @sections.save 
    render json: serialize_model(@section), status: :created 
    else 
    render json: @section.errors, status: :unproccessable_entity 
    end 
end 

serialize_model просто помощником для JSONAPI::Serializer.serialize

Вот мои текущие тесты RSpec для этого контроллера:

describe 'POST #create' do 
    before :each do 
    @section_params = { section: { title: 'Section 1', position: 'top', instructions: 'fill it out' } } 
    post '/surveys/1/sections', @section_params.to_json, format: :json 
    end 

    it 'responds successfully with an HTTP 201 status code' do 
    expect(response).to be_success 
    expect(response).to have_http_status(201) 
    end 
end 

Я попробовал несколько разных вещей и не может понять, как это исправить. Если я сделаю сообщение с этим адресом с Postman и этой точной полезной нагрузкой json, он успешно создаст новый раздел.

Тест на получение запроса работает нормально, я просто не уверен, как обрабатывать данные запроса json с помощью rspec и jsonapi-serializer.

ответ

1

Попробуйте это. Заменить YourApiController на все, что вы назвали по своему усмотрению

describe YourApiController, type: :controller do 
    context "#create" do 
    it 'responds successfully with an HTTP 201 status code' do 
     params = { section: { title: 'Section 1', position: 'top', instructions: 'fill it out' } } 
     survey = double(:survey, sections: []) 
     sections = double(:sections) 
     section = double(:section) 
     expect(survey).to receive(:sections).and_return(sections) 
     expect(sections).to receive(:all).and_return(sections) 
     expect(sections).to receive(:save).and_return(true) 
     expect(controller).to receive(:serialize_model).with(section) 
     post :create, params, format: :json 
     expect(response).to be_success 
     expect(response).to have_http_status(201) 
     expect(assigns(:sections)).to eq sections 
    end 
    end 
end 
+0

Спасибо, это помогло! – mikeLspohn

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