Using mocha, is there a way to drown out a lot of parameters?

Suppose I have this class

class Foo def bar(param1=nil, param2=nil, param3=nil) :bar1 if param1 :bar2 if param2 :bar3 if param3 end end 

I can drown out the whole bar method using:

 Foo.any_instance.expects(:bar).at_least_once.returns(false) 

However, if I only want to stub when param1 of the bar method is correct, I could not find a way to do:

I also looked with and has_entry, and it seems that this only applies to one parameter.

I was expecting some function like this.

 Foo.any_instance.expects(:bar).with('true',:any,:any).returns(:baz1) Foo.any_instance.expects(:bar).with(any,'some',:any).returns(:baz2) 

thanks

.................................................. CHANGED THE FOLLOWING .............................................

Thanks nash

Not familiar with rspec, so I tried using unit test with any_instance and it seems to work

 require 'test/unit' require 'mocha' class FooTest < Test::Unit::TestCase def test_bar_stub foo = Foo.new p foo.bar(1) Foo.any_instance.stubs(:bar).with { |*args| args[0]=='hee' }.returns('hee') Foo.any_instance.stubs(:bar).with { |*args| args[1]=='haa' }.returns('haa') Foo.any_instance.stubs(:bar).with { |*args| args[2]!=nil }.returns('heehaa') foo = Foo.new p foo.bar('hee') p foo.bar('sth', 'haa') p foo.bar('sth', 'haa', 'sth') end end 
+6
source share
1 answer

If I'm right, it could be something like:

 class Foo def bar(param1=nil, param2=nil, param3=nil) :bar1 if param1 :bar2 if param2 :bar3 if param3 end end describe Foo do it "returns 0 for all gutter game" do foo = Foo.new foo.stub(:bar).with { |*args| args[0] }.and_return(:bar1) foo.stub(:bar).with { |*args| args[1] }.and_return(:bar2) foo.stub(:bar).with { |*args| args[2] }.and_return(:bar3) foo.bar(true).should == :bar1 foo.bar('blah', true).should == :bar2 foo.bar('blah', 'blah', true).should == :bar3 end end 
+7
source

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


All Articles