开发者

Unit Testing with Stub & Mock Example

开发者 https://www.devze.com 2023-03-06 09:15 出处:网络
I am new to testing. I want to do unit testing with st开发者_运维技巧ub & mock in visual studio 2010.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消