Stubbing RestClient response in RSpec

前端 未结 3 1923
长情又很酷
长情又很酷 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 04:04

    Using a mock for the response you can do this. I'm still pretty new to rspec and test in general, but this worked for me.

    describe "successful POST on /user/create" do
      it "should redirect to dashboard" do
        RestClient = double
        response = double
        response.stub(:code) { 200 }
        RestClient.stub(:post) { response }
    
        post '/user/create', {
          :name => "dave",
          :email => "dave@dave.com",
          :password => "another_pass"
        }
        last_response.should be_redirect
        follow_redirect!
        last_request.url.should == 'http://example.org/dave/dashboard'
      end
    end
    

提交回复
热议问题