Can JustMock return a value based on a parameter?

Using JustMock, can I arrange a layout to return something based on an input parameter?

For example, let's say that the method accepts int , I want to return this value + 1
I want the output to always be laughed at as input + 1, but I don't know the input during development.

My real use for this is with an object parameter, and I need to make fun of it to always return a new object with some of the same properties ... but I don't know how to refer to the parameters in the .Returns() section.

EDIT: More details:

Three types:
IMoneyConverter
Money
Currency

An object

A Money has two properties: decimal valueAmount and Currency valueCurrency

IMoneyConverter discloses:

 .Convert(Money valueFrom, Currency currencyTo, DateTime asOfDate) 

This method returns the converted Money object to the new Currency (currencyTo) on the specified date.

My intention is to mock IMoneyConverter so that its .Convert method returns a new Money object that has the size of the Money parameter (valueFrom) and the Currency parameter of currencyTo.

+4
source share
2 answers

I am not 100% sure, I understood the exact requirements, but this test works, and I believe that I will demonstrate how to accomplish what you want:

 [Test] public void SampleTest() { IMoneyConverter mock = Mock.Create<IMoneyConverter>(); mock.Arrange( x => x.Convert( Arg.IsAny<Money>(), Arg.IsAny<Currency>(), Arg.IsAny<DateTime>() ) ) .Returns( (Func<Money,Currency,DateTime,Money>) ( (m, c, d ) => new Money { ValueAmount = m.ValueAmount, Currency = c }) ); Money inMoney = new Money() { ValueAmount = 42 }; Currency inCurrency = new Currency(); Money outMoney = mock.Convert( inMoney, inCurrency, DateTime.Now ); Assert.AreEqual( 42, outMoney.ValueAmount ); Assert.AreSame( inCurrency, outMoney.Currency ); } public interface IMoneyConverter { Money Convert( Money valueFrom, Currency currencyTo, DateTime asOfDate ); } public class Money { public decimal ValueAmount { get; set; } public Currency Currency { get; set; } } public class Currency { } 
+5
source

Yes, it is possible, see an example.

 var foo = Mock.Create<IFoo>(); Mock.Arrange(() => foo.Echo(Arg.IsAny<int>())).Returns((int i) => ++i); 
+3
source

All Articles