Dear all, I'm new to using Moq and I cannot find the way for doing this. I've a generateId private method, called
/// <summary>
/// Generates a call Id for external interfaces
/// </summary>
/// <returns></returns>
private string GenerateCallId()
{
return "EX" + SharedServicesClientProxy.Instance.GenerateId().ToString();
}
I wanted to unit test this method, and for that reason i need to mock the Proxy. SharedServicesClientProxy is just an object that implements the interface ISharedServices but adds the singleton. I wanted to test that all the strings correctly returned a string starting with "EX". this is my unit test, using Moq
/// <summary>
/// A test for GenerateCallId
/// A CallId for external systems should always start by "EX"
///</summary>
[TestMethod()]
[DeploymentItem("myDll.dll")]
public void GenerateCallIdTest()
{
myService_Accessor target = new myService_Accessor();
var SharedServicesClientProxy = new Mock<ISharedServices>();
SharedServicesClientProxy.Setup(x => x.GenerateId()).Returns(5396760556432785286);
string actual;
string extCallIdPrefix = "EX";
actual = target.GenerateCallId();
Assert.IsTrue(actual.StartsWith(extCallIdPrefix));
}
I guess I'm doing my mock in the wrong place?
In a more general way, how do i mock an object that is going to be called by the method I'm testing? for eg:
/// <summary>
/// dummy test
///</summary>
[TestMethod()]
[DeploymentItem("myDll.dll")]
public void Foo()
{
myService_Accessor target = new myService_Accessor();
boolexpected = false;
actual = target.Foo();
Assert.IsTrue(actual,expected);
}
/// <summary>
/// Foo
/// </summary>
/// <returns></returns>
private bool Foo()
{
return this.Bar();
}
I need to moq Bar, but where do i need to do it? Thanks, Seb