Moq argument capture suggestion

199 views
Skip to first unread message

Ajai

unread,
Mar 4, 2013, 12:00:22 PM3/4/13
to moq...@googlegroups.com
Hi All

Just playing with Moq, and needed to capture arguments passed to methods.

I saw discussions related to using It.IsAny and Callback to capture arguments. But felt this is was a bit cleaner...

var arg = new Arg<int>();

mock.Setup(m => m.Foo( arg.Capture() ));

Thanks

Ajai

public class Arg<T>
{
public T Value { get; set; }

public static implicit operator T(Arg<T> arg) { return arg.Value; }

public T Capture()
{
return Match<T>.Create((T value) => { Value = value; return true; });
return default(T);
}
}

Vicenç Garcia

unread,
Mar 4, 2013, 5:01:27 PM3/4/13
to moq...@googlegroups.com
Don't you could achieve the same behavior with It.Is ?



--
--
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.
 
 



--
Vicenç García
http://geeks.ms/blogs/devnettips
http://www.linkedin.com/in/vgaltes
http://www.twitter.com/vgaltes

Ajai

unread,
Mar 5, 2013, 2:38:40 AM3/5/13
to moq...@googlegroups.com
Yes, but it is more verbose, and not obvious what it is trying to do...

int arg = 0;

mock.Setup(m => m.Foo( It.Is<int>(i => { arg = i; return true; } ));

And sometimes verifying arguments is not a simple check like It.Is<int>(i => i == 10), we might need to capture more complex object.

A Dictionary<byte[], Dictionary<string, List<Mutation>>> in my case :)

Ajai

Daniel Cazzulino

unread,
Mar 5, 2013, 6:56:43 AM3/5/13
to moqdisc

Wow, that's nice!

Should we have it built in?

/kzu from mobile

Reply all
Reply to author
Forward
0 new messages