Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec: Helper method not being recognized

Tags:

ruby

rspec

rspec2

In the code below, I am getting an error on the line marked with a comment (near the bottom of the snippet): undefined methodmock_transaction_input'`

I don't understand why, since the method mock_transaction_input is defined above. I can't find the difference between what I'm doing and the first example in this article..

Any help is much appreciated!

require 'spec_helper'

describe Transaction do

    def mock_transaction_input input
        ret = double "TransactionInput"
        input.each do |method, mock_return| 
            ret.stub(method).and_return(mock_return)
        end
        ret
    end

    describe "basic calculations" do

        examples = {
            :example_one => {
                :in => {:amount => 10, :alcohol => 5, :tip => 2, :anywhere_credit => 3, :merchant_credit => 1, :initial_subsidy => 0.25, :initial_subsidy_per => 1, :repeat_subsidy => 0, :repeat_subsidy_per => 0, :deal_subsidy => 0, :our_cut => 0.2, :tax_rate => 0.0825, :cc_fee_waived? => false, :cut_on_tax? => true, :cut_on_tip? => true, :deal_redeemed? => false},
                :out => { :our_cut => -0.11 , :merchant_cut => 12.58, :gross_sale => 17, :net_sale => 16.25, :merchant_cc_fee => 0.42}
            },

        }

        examples.each do |name, input_output| 

            input = input_output[:in]
            output = input_output[:out]

            # ERROR HERE
            transaction = Transaction.new(mock_transaction_input input)

            output.each do |method, expected_return|
                it "should return #{expected_return} when #{method} is called" do
                    transaction.send(method).should == expected_return
                end
            end
        end
    end
end
like image 345
Jonah Avatar asked Dec 17 '25 03:12

Jonah


1 Answers

The quick answer - you can use mock_transaction_input method only in it or specify context. In order to user this method in the example group you have to define this method as a class method def self.mock_transaction_input.

Check the docs: https://www.relishapp.com/rspec/rspec-core/docs/helper-methods/define-helper-methods-in-a-module

like image 71
luacassus Avatar answered Dec 19 '25 21:12

luacassus