Stubbing RestClient response in RSpec

前端 未结 3 1932
长情又很酷
长情又很酷 2021-02-20 03:18

I have the following spec...

  describe \"successful POST on /user/create\" do
    it \"should redirect to dashboard\" do
      post \'/user/create\', {
                 


        
3条回答
  •  时光取名叫无心
    2021-02-20 03:55

    Instance doubles are the way to go. If you stub a method that doesn't exist you get an error, which prevents you from calling an un-existing method in production code.

          response = instance_double(RestClient::Response,
                                     body: {
                                       'isAvailable' => true,
                                       'imageAvailable' => false,
                                     }.to_json)
          # or :get, :post, :etc
          allow(RestClient::Request).to receive(:execute).and_return(response)
    

提交回复
热议问题