Hi, Jeremy.
Thanks for the reply, but I still can't get this to work. The
extension you suggested didn't work because the compiler was asking
for the type parameter on the "this MoqAutoMocker mocker" parameter,
so I changed the extension method to
public static class MoqAutoMockerExtensions
{
public static Mock<T> GetMock<T>(this MoqAutoMocker<T> mocker)
{
return mocker.Get<T>() as Mock<T>;
}
}
and the compiler was happy.
This of course didn't work as it was always trying to return the mock
for the class under test. So if I had the code...
var autoMocker = new MoqAutoMocker<SignInController>();
autoMocker.GetMock<ISecurityService>().Setup(ss => ss.ValidateLogin
(It.IsAny<string>(), It.IsAny<string>())).Returns
(ValidateLoginResult.Success);
I would get an invalid cast exception of "can't cast type of
Mock<SignInController> to Mock<ISecurityService>".
So then I fumbled around with the generics syntax for a while until I
could find something the compiler was happy with and came up with
this...
public static class MoqAutoMockerExtensions
{
public static Mock<R> GetMock<R, T>(this MoqAutoMocker<T> mocker)
where R : class where T : class
{
return mocker.Get<R>() as Mock<R>;
}
}
This made my call to GetMock a little more clumsy, as below
var autoMocker = new MoqAutoMocker<SignInController>();
autoMocker.GetMock<ISecurityService, SignInController>().Setup(
ss => ss.ValidateLogin(It.IsAny<string>(), It.IsAny<string>
())).Returns(ValidateLoginResult.Success);
since I have to include the SignInController again in the call to
GetMock, but again, it made the compiler happy.
The problem is that this still doesn't work. Trying to cast the
mocker.Get<R> as Mock<R> just doesn't work. I keep getting null
reference exceptions. If I look at the return value of mocker.Get<R>
in the debugger it shows a return type of
{ISecurityServiceProxyec205051a8d74b03a465cde5c4172efc} which is the
proxy object created by Moq.
There's seemingly no way for me to get to the Mock<R> interface this
way. I also tried looking at some of the other methods and properties
exposed by MoqAutoMocker<T> and those didn't seem to get me anywhere
either. I'm admittedly new to all this so I'm not sure if I'm missing
something obvious or not.
At this point I'd take any way you can suggest to get access to the
Mock<T> objects that the AutoMocker creates for the dependencies. I
just can't seem to figure out how to do it.
Thanks!
Jamie
On Jun 3, 3:30 pm, "Jeremy D. Miller" <
jeremydmil...@yahoo.com> wrote:
> Nope, you're doing fine. With RhinoMocks, all the Setup/Expect business is an extension method against "object", so Get<T>() works just fine in that scenario (and you've probably guessed that the AutoMocker was written with RhinoMocks in mind).
>
> In your testing code, why don't you add this:
>
> public static class MoqAutoMockerExtensions
> {
> public static Mock<T> GetMock<T>(this MoqAutoMocker mocker)
> {
> return mocker.Get<T>() as Mock<T>;
> }
>
> }
>
> and then always use GetMock<T>() for Moq usage.
>
> Jeremy D. Miller
> The Shade Tree Developer
>
jeremydmil...@yahoo.com
>
> ________________________________
> From: JamieG <
jamie.gai...@gmail.com>