Moq 在.net Core 单元测试中的使用

Moq,主要用来伪造接口的实现类,实现方法,属性

moq

The most popular and friendly mocking framework for .NET

What?

Moq (pronounced "Mock-you" or just "Mock") is the only mocking library for .NET developed from scratch to take full advantage of .NET Linq expression trees and lambda expressions, which makes it the most productive, type-safe and refactoring-friendly mocking library available. And it supports mocking interfaces as well as classes. Its API is extremely simple and straightforward, and doesn't require any prior knowledge or experience with mocking concepts.

Github:https://github.com/moq/moq4

常用方法汇总:

   定义Mock对象

  1.new Mock<IFoo>();

   设置mock对象返回值,返回固定值或者ThrowException

  1.mockInstance.SetUp().Returns() 

    mock.Setup(foo => foo.DoSomething("ping")).Returns(true);

  2.mockInstance.SetUpSet()

  mock.SetupSet(s => s.Name = "zhangsan");

 3.mockInstance.SetupProperty()    SetupProperty方法返回mockInstance对象,可以继续.方法,方便多次设置

  mock
   .SetupProperty(f => f.Name, "haha")
   .SetupProperty(f => f.Add(6), false)
   .SetupProperty(f => f.Bar, new Bar());

<h6>合计

猜你喜欢

转载自www.cnblogs.com/mailaidedt/p/10215798.html