RSpec mock or stub super in the model

How to test this tiny part of the module, with super? (superclass - testing / integration action_dispatch-3.0.1 ...) The module is included in the specifications / requests for intercepting a message:

module ApiDoc
  def post(path, parameters = nil, headers = nil)
    super
    document_request("post", path, parameters, headers) if ENV['API_DOC'] == "true"
  end
  ...
end

I do not want it to run ActionDispatch :: Integration - independently, but I do not know how to mock or stub superto unit test it.

The module is used only in specifications and will have 100% testing coverage, which proves these types of indicators as useless. I need unit test it.

An example, if necessary, is how I use the ApiDoc module

require 'spec_helper'

describe "Products API" do
  include ApiDoc  ############## <---- This is my module
  context "POST product" do
    before do
      @hash = {:product => {:name => "Test Name 1", :description => "Some data for testing"}}
    end

    it "can be done with JSON" do
      valid_json = @hash.to_json
      ############### the following 'post' is overriden by ApiDoc
      post("/products.json",valid_json,
       {"CONTENT_TYPE" => "application/json",
        "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})
      response.should be_success
    end
  end
end
+3
source share
2 answers

, 'super'

ActionDispatch::Integration.any_instance.should_receive(:post)

ApiDock , post alias_method_chain:

ActionDispatch::Integration.instance_eval do
  def post_with_apidoc(path, parameters = nil, headers = nil)
    post_without_apidoc
    if ENV['API_DOC'] == "true"
      document_request("post", path, parameters, headers)
    end
  end
  alias_method_chain :post, :apidoc
end
+4

. .

require 'spec_helper'

describe 'ApiDoc' do
  include ApiDoc

  it "should pass the post to super, ActionDispatch" do
    @path = "path"
    @parameters = {:param1 => "someparam"}
    @headers = {:aheader => "someheaders"}
    ActionDispatch::Integration::Session.any_instance.expects(:post).with(@path, @parameters, @headers)
    post(@path, @parameters, @headers)
  end
end

class DummySuper
  def post(path, parameters=nil, headers=nil)
    #How to verify this is called?
  end
end
class Dummy < DummySuper
  include ApiDoc
end

describe Dummy do

  it "should call super" do
    subject.expects(:enabled?).once.returns(true)
    #how to expect super, the DummySuper.post ?
    path = "path"
    parameters = {:param1 => "someparam"}
    headers = {:aheader => "someheaders"}
    subject.expects(:document_request).with("post", path, parameters, headers)
    subject.post(path, parameters, headers)
  end
end

ApiDoc.

module ApiDoc
  def enabled?
    ENV['API_DOC'] == "true"
  end

  def post(path, parameters = nil, headers = nil)
    super
    document_request("post", path, parameters, headers) if enabled?
  end

private
  def document_request(verb, path, parameters, headers)
  ...
  end
end

super.post , , Dummy.

+1

Source: https://habr.com/ru/post/1777078/


All Articles