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
0 votes
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
public class Form1Test
/// <summary>
/// Initialize TypeMock before each test
/// </summary>
public void Start()
/// <summary>
/// We will verify that the mocks have been called correctly at the end of each test
/// </summary>
public void Finish()

/// <summary>
/// We will test mocking an interface
/// </summary>
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);

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

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 version of TypeMock.
asked by kmsuresh (600 points)

1 Answer

0 votes
Hi kmsuresh,

According to the error message you are getting, the call is not being recorded properly. Would it be possible for you to post the code for MyMathClass here so I can try to recreate this behavior?

Typemock Support
answered by doron (16.5k points)