Mock.Of and function parameters in setup

530 views
Skip to first unread message

Standa K.

unread,
Feb 22, 2013, 11:17:56 AM2/22/13
to moq...@googlegroups.com
Hello,

just today I discovered this Mock.Of<Type>() style of creating mocks when I was searching for a fluent Moq interface (that is, one which would allow me to create the mock and specify its behavior in one step unlike the Setup() method I've been using so far).

However, I can't figure out how to specify a behavior of a mock's method call which depends on a parameter.

I've read through the "Mock.Of - how to specify behavior?" thread, but all of the examples given there were constant functions (their output didn't depend on the actual parameters passed).

Is there way to use parameters when using Mock.Of?


Daniel Cazzulino

unread,
Feb 22, 2013, 11:22:00 PM2/22/13
to moqdisc
Don't think that's supported, nop... 

If you can guess a syntax that would compile where you could use the parameters, we can spike implementing it :)


/kzu

--
Daniel Cazzulino




--
--
Post: moq...@googlegroups.com
Unsubscribe: moqdisc-u...@googlegroups.com
---
You received this message because you are subscribed to the Google Groups "Moq Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to moqdisc+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ale Miralles

unread,
Feb 28, 2013, 8:37:22 AM2/28/13
to moq...@googlegroups.com

Hi Standa, you mean something like this:

 
public interface IFoo {
    int Bar(string arg);
}
 
[Test]
public void ResultBasedOnInputParams() {
    var mock = Mock.Of<IFoo>(f => 
           f.Bar("foo") == 123 
        && f.Bar("bar") == 456 
        && f.Bar("baz") == 789);
            
    Assert.AreEqual(123, mock.Bar("foo"));
    Assert.AreEqual(456, mock.Bar("bar"));
    Assert.AreEqual(789, mock.Bar("baz"));
    Assert.AreEqual(0, mock.Bar("anything else"));
}

 

Not sure if it is what you are looking for, but this way you can specify different results based on the value of an input params….

 

Ale Miralles.

http://amiralles.com.ar

http://blog.amiralles.com.ar

Reply all
Reply to author
Forward
0 new messages