When taunts, how can I return one of the arguments that was accepted?

I have a class method that I would like to make fun of and return one of the arguments that were passed. Something like this in my code:

converted_data = Myclass.convert(arg, some_other_arg, data) 

And in my test, I would like to be able to do the following (although this does not work).

 Myclass.should_receive(:convert).with(*args).and_return(args[2]) 

Thus, the method actually does nothing! If I run, as described above, I get an error message that it does not know which arguments should return.

+4
source share
2 answers

I found the answer:

 @something.should_receive(:method).with(any_args()).and_return { |*args| args } 

To return a specific argument, select it from the array in the and_return block! So for your example, this would be:

 Myclass.should_receive(:convert).with(any_args()).and_return { |*args| args[2] } 

You probably want to do this in an instance of Myclass .

+2
source

Use #and_return with a block:

 Myclass.should_receive(:convert).with(*args).and_return {|args| args[2]} 
+1
source

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


All Articles