I have created an interop for a com server, which basically does ADD & Sub operation on two numbers.
I have written a .Net C# client, which creates instance of above server on the constructor and on ADD_Click() & Sub_click() buttons, my server was invoked to perform ADD & Sub operation.
I was trying to mock my server and I don't my server functions to be called. I am using natual mocking and I have written code as below,
using MathServer;
using MyMathCompClient;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TypeMock;
//using NUnit.Framework;
using System;
namespace MathComUnitTest
{
/// <summary>
///This is a test class for MyMathCompClient.Form1 and is intended
///to contain all MyMathCompClient.Form1 Unit Tests
///</summary>
[TestClass]
public class Form1Test
{
/// <summary>
/// Initialize TypeMock before each test
/// </summary>
[TestInitialize]
public void Start()
{
MockManager.Init();
}
/// <summary>
/// We will verify that the mocks have been called correctly at the end of each test
/// </summary>
[TestCleanup]
public void Finish()
{
MockManager.Verify();
}
/// <summary>
/// We will test mocking an interface
/// </summary>
[TestMethod]
public void MockMathServer()
{
// Let create a mock object for IProduct
MyMathClass mockedServer = RecorderManager.CreateMockedObject(typeof(MyMathClass)) as MyMathClass;
// Start Mocking
using (RecordExpectations recorder = new RecordExpectations())
{
recorder.ExpectAndReturn(mockedServer.Add(), 100);
recorder.ExpectAndReturn(mockedServer.Sub(), 300);
}
// END OF MOCK SETUP
Form1 myTestObject = new Form1();
MyMathCompClient_Form1Accessor accessor = new MyMathCompClient_Form1Accessor(myTestObject);
accessor.addButton_Click(null, null);
int myResult = int.Parse(accessor.result.Text);
// lets test our mocked object
Assert.AreEqual(100,myResult);
}
}
}
Question I have is,
I was getting an execption as below when ExpectAndRetur() get called,
"
*** Cannot use Return in this sequence, there must be a mocked statement first
Perhaps you are trying to mock a method from mscorlib"
Any idea, what mistake I am making here?
I am using 4.1.0.0 version of TypeMock.