chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I'm trying to mock a WCF call. The call to the WCF never gets mocked and it calls the WCF service everytime.

[Isolated]
        public void ValidateCustomerPolicyNumberTest(string zipcode, string policyNumber, string policyType, bool mockReturnValue, bool expectedValue)
        {
            bool actualValue;

            var fakeWcfService = Isolate.Fake.Instance<CustomerPolicyDataClient>();
            Isolate.WhenCalled(() => fakeWcfService.ValidateCustomerPolicyExists(zipcode, policyNumber)).WillReturn(mockReturnValue);
            Isolate.Swap.NextInstance<CustomerPolicyDataClient>().With(fakeWcfService);

            actualValue = CustomerPolicy.ValidateCustomerPolicyNumber(zipcode, policyNumber, policyType);

            Assert.AreEqual(expectedValue, actualValue);
        }


public static class CustomerPolicy
    {

        public static bool ValidateCustomerPolicyNumber(string zipCode, string policyNumber, string policyType)
        {
            bool isValid = false;
            PolicyTypes policyTypes = new PolicyTypes();

            if (policyTypes.ValidatePolicyType(policyNumber, policyType))
            {
                CustomerPolicyDataClient client = new CustomerPolicyDataClient();

                isValid = client.ValidateCustomerPolicyExists(zipCode, policyNumber);
            }

            return isValid;
        }

        
    }
[/code]
asked by jimwieber (1.2k points)

4 Answers

0 votes
Hi Jim,

What test framework are you using? Is the test framework calling the method directly, or is it called from another test? If the method is called from another test, try decorating that test with the Isolated attribute.

Until you get back, did you get a chance to read our WCF with Isolator walkthrough blog posts? You can find them in our blog under http://blog.typemock.com/search/label/WCF, and they should point you in the right way.

Please let me know if this helps.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
I'm using Gallio MbUnit 3.2 with Isolotor 2010. The mothed is being called from within the method being tested. I'm just trying to mock the WCF method call to return a true or false so that I don't need to be connected to the WCF service.

I did read the WCF documentation but I didn't think any of the examples werer mocking the WCF method call.
answered by jimwieber (1.2k points)
0 votes
Hi,

Can it be that there's another CustomerPolicyDataClient in the code before you get to the one you want to fake?
You can check this by replacing the call to: Isolate.Swap.NextInstance<CustomerPolicyDataClient>().With(fakeWcfService);
with Isolate.Swap.AllInstances<CustomerPolicyDataClient>().With(fakeWcfService);

Please let me know if it helps.
answered by ohad (35.4k points)
0 votes
I was referencing the wrong CustomerPolicyDataClient. I had added a service reference in my test project to the CustomerPolicyDataClient which was causing the local CustomerPolicyDataClient in my test project to be mocked instead of the one in the method I was calling in the other project. Once I removed the Service reference, it worked correctly.
answered by jimwieber (1.2k points)
...