Sebastien
Sebastien

Reputation: 6660

How do I stub a method of an instance only if a specific instance variable has a value?

I have an object MyObject:

class MyObject

  def initialize(options = {})
    @stat_to_load = options[:stat_to_load] || 'test'
  end

  def results
   []
  end
end

I want to stub the results method only if stat_to_load = "times". How can I do that? I tried:

MyObject.any_instance.stubs(:initialize).with({
  :stat_to_load => "times"
}).stubs(:results).returns(["klala"])

but it does not work. Any idea?

Upvotes: 6

Views: 1605

Answers (3)

sank
sank

Reputation: 1

You could use plain old Ruby inside your test to achieve this.

MyObject.class_eval do
  alias_method :original_results, :results
  define_method(:results?) do
    if stats_to_load == "times"
      ["klala"]
    else
      original_results
    end
  end
end

Upvotes: 0

Sandip Ransing
Sandip Ransing

Reputation: 7733

Try out below, this should work as expected:

Here, Basically we are actually stubbing new instance getting created and also stubbing results method of the instance which is getting returned.

options = {:stat_to_load =>  "times"}
MyObject.stubs(:new).with(options)
                    .returns(MyObject.new(options).stubs(:results).return(["klala"]))

Upvotes: 0

gregates
gregates

Reputation: 6724

So, I think there is probably a simpler way to test what you're trying to test, but without more context I don't know what to recommend. However, here is some proof-of-concept code to show that what you want to do can be done:

describe "test" do
  class TestClass
    attr_accessor :opts
    def initialize(opts={})
      @opts = opts
    end

    def bar
      []
    end
  end
  let!(:stubbed) do
    TestClass.new(args).tap{|obj| obj.stub(:bar).and_return("bar")}
  end
  let!(:unstubbed) { TestClass.new(args) }

  before :each do
    TestClass.stub(:new) do |args|
      case args
      when { :foo => "foo" }
        stubbed
      else
        unstubbed
      end
    end
  end

  subject { TestClass.new(args) }

  context "special arguments" do
    let(:args) { { :foo => "foo" } }
    its(:bar) { should eq "bar" }
    its(:opts) { should eq({ :foo => "foo" }) }
  end

  context "no special arguments" do
    let(:args) { { :baz => "baz" } }
    its(:bar) { should eq [] }
    its(:opts) { should eq({ :baz => "baz" }) }
  end

end

test
  special arguments
    bar
      should == bar
    opts
      should == {:foo=>"foo"}
  no special arguments
    bar
      should == []
    opts
      should == {:baz=>"baz"}

Finished in 0.01117 seconds
4 examples, 0 failures

However I'm making a lot of use of special subject/let context blocks here. See http://benscheirman.com/2011/05/dry-up-your-rspec-files-with-subject-let-blocks/ for more on that subject.

Upvotes: 0

Related Questions