简体   繁体   中英

Rails how to mock Http Post Request

In my controller I have made this post request with parameters and it worked perfectly

parameters = {'email' => "email", 'password' => "password"}      
response = Net::HTTP.post_form(URI.parse('http://example.com/unsubscriptions'), {'q'=>parameters})
      if response.is_a?(Net::HTTPSuccess)
        @msg = "You have successfully unsubscribed."
      else
        @msg = "Resource not found."
      end

In specs controller this is the code

   it "should return success message on a successfull Post request to api" do
              parameters = { "email" => 'email', "password" => "password" }
              Net::HTTP.should_receive(:post_form).with(
                  URI.parse("http://example.com/unsubscriptions"), "q" => parameters
              ).and_return(Net::HTTPSuccess)
              post :unsub_uk, parameters
              #assigns(:msg).should eq("You have successfully unsubscribed.")
              response.should be_success
    end

I am not being able mock this Net::HTTP.post_form request. Any idea or any other way to do this task? Thanks in advance.

Just use gem webmock .

Believed solves the problem, because the return should be Net::HTTPSuccess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM