mardi 12 mars 2019

How to cleanly stub out REST client when in test environment

I have a basic model like the following

class MyModel
    def initialize(attrs)   
        @attrs = attrs
        @rest_client = Some::REST::Client.new 
    end

    def do_a_rest_call(some_str)
      @rest_client.create_thing(some_str)
    end
end

For testing purposes, I don't want @rest_client to make remote calls. Instead, in a test environment, I just want to make sure that @rest_client gets called with a specific some_str when it goes through certain branches of code.

In an ideal world, I'd have an assertion similar to:

expect(my_model_instance).to.receive(do_a_rest_call).with(some_str) where in the test I will pass some_str to make sure it's the right one.

What's the best way to do this using RSpec 3.8 and Rails 5.2.2?

Aucun commentaire:

Enregistrer un commentaire