jeudi 30 juin 2016

How to mock classes in Rails controller with mocha

I'm having a hard time understanding how to use mocha mocking library for certain type of unit tests in Rails.

I have a controller, which initializes an object from a helper library, and then calls a function on it. My code looks similar to

class ObjectsController < ApplicationController
  before_action :set_adapter

  def index
    response = @adapter.get_objects

    render json: response
  end

  private
    def set_adapter
      arg = request.headers["X-ARG"]
      @adapter = Adapter::Adapter.new(arg)
    end
end

In my tests, I want mock the adapter to make sure that the get_objects() method is called. I'm trying to figure out what is the best way to implement this kind of tests, but I seem to be stuck on getting the idea on how to mock an existing object within a class.

Can anyone help me out?

Aucun commentaire:

Enregistrer un commentaire