This project is read-only.

Horrifyingly Noobish Question

Nov 28, 2011 at 11:27 PM

I can't figure out why I'm getting a cast exception on this line:

            IRestServiceClient client = new RestServiceClient((IRestRequestBuilderFactory)restRequestBuilderFactory);

The mock is declared here:

        private Mock<IRestRequestBuilderFactory> factory;

and created in the SetUp method:

            factory = mockFactory.CreateMock<IRestRequestBuilderFactory>();

I must be missing something fundamental: if a mock can't be cast to the interface it's supposedly mocking, what kind of a mock is it?

I'm using NMock3 with Visual Studio 2008, so I can't run the tutorial.  I can't find any other documentation online, including the legendary NMock3 cheat sheet.


Complete listing:

using System;
using System.Collections.Generic;
using System.Text;
using GPICore.Repository.WebService;
using NMock2;
using NUnit.Framework;

namespace NorthernTrust.GPI.Home.Test.UnitTest.Repository.WebService

    public class NMockTempTest
        private static readonly RestResourceType ResourceType = RestResourceType.SplashMessage;
        private static readonly RestResourceMethod Method = RestResourceMethod.GET;

        private MockFactory mockFactory;
        private Mock<IRestRequest> request;
        private Mock<IRestRequestBuilderFactory> factory;
        private Mock<IRestRequestBuilder> builder;

        public void Setup()
            mockFactory = new MockFactory();
            request = mockFactory.CreateMock<IRestRequest>();
            factory = mockFactory.CreateMock<IRestRequestBuilderFactory>();
            builder = mockFactory.CreateMock<IRestRequestBuilder>();

        public void TheServiceBuildsTheRequest()
            factory.Expects.One.MethodWith(f => f.GetBuilder()).WillReturn(builder as IRestRequestBuilder);

            builder.Expects.One.MethodWith(b => b.ResourceType(ResourceType))
                .WillReturn(builder as IRestRequestBuilder);
            builder.Expects.One.MethodWith(b => b.Method(Method))
                .WillReturn(builder as IRestRequestBuilder);
            builder.Expects.One.MethodWith(b => b.Build()).WillReturn(request as IRestRequest);

            var restRequestBuilderFactory = factory;
            IRestServiceClient client = new RestServiceClient((IRestRequestBuilderFactory)restRequestBuilderFactory);

            client.Get(ResourceType, "");

The error message:
TheServiceBuildsTheRequest : Failed
System.InvalidCastException : Unable to cast object of type 'NMock2.Mock`1[GPICore.Repository.WebService.IRestRequestBuilderFactory]' to type 'GPICore.Repository.WebService.IRestRequestBuilderFactory'.
at NorthernTrust.GPI.Home.Test.UnitTest.Repository.WebService.NMockTempTest.TheServiceBuildsTheRequest() in NMockTempTest.cs: line 44
Nov 28, 2011 at 11:30 PM

Sorry for the confusion - I assigned factory to a var named restRequestBuilderFactory as part of a failed debugging attempt.

Jul 6, 2012 at 1:46 PM

Tried using the mock.MockObject property?


IRestServiceClient client = new RestServiceClient((IRestRequestBuilderFactory)restRequestBuilderFactory.MockObject);
Jul 6, 2012 at 6:34 PM

Thanks for the tip, but our benevolent overlords have made it academic:  NMock is not approved software, so we're using Moq.