Skip to content

Instantly share code, notes, and snippets.

@pierrax
Last active May 24, 2016 15:50
Show Gist options
  • Save pierrax/10e9cd24611da129467d3ea24e708891 to your computer and use it in GitHub Desktop.
Save pierrax/10e9cd24611da129467d3ea24e708891 to your computer and use it in GitHub Desktop.
Mock tests

Problem

How to test complex controller method with chained method ?

Example

  
  def update
    # ...
    user.propositions.call
    # ...
  end
  
  # ...
  def call
    # ...
    response = MyAPI::connect(object)
    Object.create(params)
    if SeviceObject.new().send
      return true
    else
      # ...
    end   
    # ...
    
  end

First try

Use classic receive and return method.

RSpec.describe "update" do
  it "calls the call method" do
    # ...
    expect_any_instance_of(User).to receive_chain_message(:propositions, :call).and_return(true)
    
    put :update, params
    #...
  end
end

Solution : use stub

  it "calls the call method" do
    # ...
    propositions = double('Proposition', call: true)
    expect_any_instance_of(User).to receive(:propositions).and_return(propositions)
    
    put :update, params
    #...
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment