Kumar Nikhil
Kumar Nikhil

Reputation: 125

Worker Spec is Failing

I have a worker and I am writing specs for success and failure condition.

if http_response.success?
  "Login Event Success"
else
  raise "Login Event Failed"
end

Error is in this format:

     "{\n \"status\" : 400,\n  
     \"error\" : \"Bad Request\",\n  \"message\" : \"ID field not be empty\",\n  \"path\" : \"/publish/event\"\n}"

Specs for that I am writing like this:

context "unsuccessful response" do
  let(:error_message) { "ID field not be empty" }
  
  before do
    stub_request(:post, "#{api}").
      with(body: params(1)).
      to_return(status: 400, body: error_message, headers: {})
  end
  
  it "returns a unsuccessful response" do
    expect(described_class.jobs.count).to eq(1)
  end
end
def params(id)
{
  processor: "ABC",
  type: "Clinic",
  date: DateTime.now.to_s,
  clinicId: id,
  ip: "127.0.0.1"
 }
end

Test Fail:

 Failures:

   1) EventWorker unsuccessful response returns a unsuccessful response
 Failure/Error: expect(described_class.jobs.count).to eq(1)
 
   expected: 1
        got: 0
 
   (compared using ==)

Stubbing I am doing is not correct or what, that I am not getting. Because this is not going in main worker in else condition.

Could some one help me in fixing this negative scenario.

Upvotes: 0

Views: 57

Answers (1)

Schwern
Schwern

Reputation: 164829

Assuming that stub_request is coming from webmock...

You never make a request. stub_request does not make a request. You need to make a request.

Upvotes: 0

Related Questions