Unit Testing with Stub & Mock Example
I am new to testing.
I want to do unit testing with st开发者_运维技巧ub & mock in visual studio 2010.
Please provide any example or step by step guide for doing this.
Thanks
Here's example how to use Moq
public interface IService
{
string DoSomething(int i);
}
public class MyClass
{
private readonly IService service;
public MyClass(IService service)
{
this.service = service;
}
public string void Print()
{
var message = service.DoSomething();
Console.WriteLine(message );
return message;
}
}
[Test]
public void TestSomething()
{
var service = new Mock<IService>();
service.Setup(x => x.DoSomething(It.IsAny<int>())).Returns("bla-bla");
Assert.AreEqual("bla-bla", new MyClass(service).Print());
}
NUnit nmock Moq DotNetMock these open source tool all provide mock funciton.
精彩评论