Beginner Problems with WebMock and Rspec

74 views
Skip to first unread message

John

unread,
Dec 17, 2014, 8:16:23 AM12/17/14
to webmoc...@googlegroups.com

This is my controller code

controller code :-

  def test

   image= Image.new(params)

   image.save


    image.rest_service

  end

Model

  def rest_service


   RestClient.post('i am here')


   end

it 'should return json with response code 200' do

     post '/orders/test', { order: {agreement_id: '101}}.to_json, get_headers(:private)

    expect(last_response.status).to eq(200)

  end


Problem

I have to stub rest_service request ,through webmock . but not sure where and how to add code 

Aleksey Zapparov

unread,
Dec 18, 2014, 8:27:02 AM12/18/14
to webmoc...@googlegroups.com
There are two options:

You can "spy" on requests or "mock" them. Or you can assert (expect) them to happen. In your case, it looks like you need just mock, which is pretty simple:

it 'should return json with response code 200' do


  stub_request
(:post, 'i am here').to_return(:body => 'mocked response')
  post
('/orders/test', { order: {agreement_id: '101}}.to_json, get_headers(:private))
  expect(last_response.status).
to eq(200)
end

Reply all
Reply to author
Forward
0 new messages