using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using WCFClient.WCFServiceReference; using WCFClient; using Microsoft.Practices.Unity; namespace WCFClientTest { [TestClass] public class WCFClientUnitTest { [TestMethod] public void MockWCFService() { string val = "MockedValue"; string actualRetVal; // mock wcf interface Mock<IWCFService> wcfMock = new Mock<IWCFService>(); // setup for wcf service GetData method wcfMock.Setup<string>(s => s.GetData(It.IsAny<int>())).Returns(val); // create object to register with IoC IWCFService wcfMockObject = wcfMock.Object; // register instance UnityHelper.IoC = new UnityContainer(); UnityHelper.IoC.RegisterInstance<IWCFService>(wcfMockObject); // create ServiceAgent object using parameterized constructor (for unit test) WCFServiceAgent serviceAgent = new WCFServiceAgent(true); // method call to be tested actualRetVal = serviceAgent.HitWCFService(); // verify if the expected method called during test or not wcfMock.Verify(s => s.GetData(It.IsAny<int>()), Times.Exactly(1)); Assert.AreEqual("MockedValue", actualRetVal, "Not same."); } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)