C# Moq

来源:https://www.cnblogs.com/lldwolf/archive/2019/11/07/10947060.html
-Advertisement-
Play Games

Moq 1 My Cases 1.1 簡單入門 2 Reference 2.1 Methods 2.2 Matching Arguments 2.3 Properties 2.4 Events 2.5 Callbacks 2.6 Verification 2.7 Customizing Mock B ...


Moq


1 My Cases

        1.1 簡單入門

2 Reference

        2.1 Methods

        2.2 Matching Arguments

        2.3 Properties

        2.4 Events

        2.5 Callbacks

        2.6 Verification

        2.7 Customizing Mock Behavior

        2.8 Miscellaneous

        2.9 Advanced Features

        2.10 LINQ to Mocks

3 FAQ

        3.1 static class/method


1 My Cases

1.1 簡單入門

// 假定我有一個 MyFactory 用來創建 MyInterface 實例
// 創建 MyFactory 的 Mock 對象
var mockFactory = new Mock<MyFactory>();

// 創建 MyInterface 的 Mock 實例
var mockInstance = new Mock<MyInterface>();

// 使用 Moq 實現如果調用 MyInstance.DoSomething(bool) 方法無論傳入參數為何值一概拋出 MyException 異常
mockInstance.Setup(c => c.DoSomething(It.IsAny<bool>()))
    .Throws(new MyException("my exception message"));

// 使用 Moq 實現 MyFactory 的 Mock 實例第一次調用 CreateInstance(string) 方法時返回 MyInterface 的 Mock 實例
// 第二次及以後調用則返回真正的 MyInstance 實例
mockFactory.SetupSequence(f => f.CreateInstance(It.IsAny<string>()))
    .Returns(mockInstance.Object)
    .Returns(new MyInstance("real object"));

client.Factory = mockFactory.Object;

2 Reference

Please refer to Moq Quickstart

Moq is intended to be simple to use, strongly typed (no magic strings!, and therefore full compiler-verified and refactoring-friendly) and minimalistic (while still fully functional!).

2.1 Methods

Methods Mock

using Moq;

// Assumptions:

public interface IFoo
{
    Bar Bar { get; set; }
    string Name { get; set; }
    int Value { get; set; }
    bool DoSomething(string value);
    bool DoSomething(int number, string value);
    string DoSomethingStringy(string value);
    bool TryParse(string value, out string outputValue);
    bool Submit(ref Bar bar);
    int GetCount();
    bool Add(int value);
}

public class Bar 
{
    public virtual Baz Baz { get; set; }
    public virtual bool Submit() { return false; }
}

public class Baz
{
    public virtual string Name { get; set; }
}


var mock = new Mock<IFoo>();
mock.Setup(foo => foo.DoSomething("ping")).Returns(true);


// out arguments
var outString = "ack";
// TryParse will return true, and the out argument will return "ack", lazy evaluated
mock.Setup(foo => foo.TryParse("ping", out outString)).Returns(true);


// ref arguments
var instance = new Bar();
// Only matches if the ref argument to the invocation is the same instance
mock.Setup(foo => foo.Submit(ref instance)).Returns(true);


// access invocation arguments when returning a value
mock.Setup(x => x.DoSomethingStringy(It.IsAny<string>()))
		.Returns((string s) => s.ToLower());
// Multiple parameters overloads available


// throwing when invoked with specific parameters
mock.Setup(foo => foo.DoSomething("reset")).Throws<InvalidOperationException>();
mock.Setup(foo => foo.DoSomething("")).Throws(new ArgumentException("command"));


// lazy evaluating return value
var count = 1;
mock.Setup(foo => foo.GetCount()).Returns(() => count);


// returning different values on each invocation
var mock = new Mock<IFoo>();
var calls = 0;
mock.Setup(foo => foo.GetCount())
	.Returns(() => calls)
	.Callback(() => calls++);
// returns 0 on first invocation, 1 on the next, and so on
Console.WriteLine(mock.Object.GetCount());

2.2 Matching Arguments

Arguments Mock

// any value
mock.Setup(foo => foo.DoSomething(It.IsAny<string>())).Returns(true);


// any value passed in a `ref` parameter (requires Moq 4.8 or later):
mock.Setup(foo => foo.Submit(ref It.Ref<Bar>.IsAny)).Returns(true);


// matching Func<int>, lazy evaluated
mock.Setup(foo => foo.Add(It.Is<int>(i => i % 2 == 0))).Returns(true); 


// matching ranges
mock.Setup(foo => foo.Add(It.IsInRange<int>(0, 10, Range.Inclusive))).Returns(true); 


// matching regex
mock.Setup(x => x.DoSomethingStringy(It.IsRegex("[a-d]+", RegexOptions.IgnoreCase))).Returns("foo");

2.3 Properties

  • Common

    Arguments Mock

    mock.Setup(foo => foo.Name).Returns("bar");
    
    
    // auto-mocking hierarchies (a.k.a. recursive mocks)
    mock.Setup(foo => foo.Bar.Baz.Name).Returns("baz");
    
    // expects an invocation to set the value to "foo"
    mock.SetupSet(foo => foo.Name = "foo");
    
    // or verify the setter directly
    mock.VerifySet(foo => foo.Name = "foo");
    
  • Setup a property so that it will automatically start tracking its value (also known as Stub)

    Arguments Mock

    // start "tracking" sets/gets to this property
    mock.SetupProperty(f => f.Name);
    
    // alternatively, provide a default value for the stubbed property
    mock.SetupProperty(f => f.Name, "foo");
    
    
    // Now you can do:
    
    IFoo foo = mock.Object;
    // Initial value was stored
    Assert.Equal("foo", foo.Name);
    
    // New value set which changes the initial value
    foo.Name = "bar";
    Assert.Equal("bar", foo.Name);
    
  • Stub all properties on a mock (not available on Silverlight)

    mock.SetupAllProperties();
    

2.4 Events

Events Mock

// Raising an event on the mock
mock.Raise(m => m.FooEvent += null, new FooEventArgs(fooValue));

// Raising an event on the mock that has sender in handler parameters
mock.Raise(m => m.FooEvent += null, this, new FooEventArgs(fooValue));

// Raising an event on a descendant down the hierarchy
mock.Raise(m => m.Child.First.FooEvent += null, new FooEventArgs(fooValue));

// Causing an event to raise automatically when Submit is invoked
mock.Setup(foo => foo.Submit()).Raises(f => f.Sent += null, EventArgs.Empty);
// The raised event would trigger behavior on the object under test, which 
// you would make assertions about later (how its state changed as a consequence, typically)

// Raising a custom event which does not adhere to the EventHandler pattern
public delegate void MyEventHandler(int i, bool b);
public interface IFoo
{
  event MyEventHandler MyEvent; 
}

var mock = new Mock<IFoo>();
...
// Raise passing the custom arguments expected by the event delegate
mock.Raise(foo => foo.MyEvent += null, 25, true);

2.5 Callbacks

Mock Callback

var mock = new Mock<IFoo>();
var calls = 0;
var callArgs = new List<string>();

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

// access invocation arguments
mock.Setup(foo => foo.DoSomething(It.IsAny<string>()))
    .Returns(true)
    .Callback((string s) => callArgs.Add(s));

// alternate equivalent generic method syntax
mock.Setup(foo => foo.DoSomething(It.IsAny<string>()))
    .Returns(true)
    .Callback<string>(s => callArgs.Add(s));

// access arguments for methods with multiple parameters
mock.Setup(foo => foo.DoSomething(It.IsAny<int>(), It.IsAny<string>()))
    .Returns(true)
    .Callback<int, string>((i, s) => callArgs.Add(s));

// callbacks can be specified before and after invocation
mock.Setup(foo => foo.DoSomething("ping"))
    .Callback(() => Console.WriteLine("Before returns"))
    .Returns(true)
    .Callback(() => Console.WriteLine("After returns"));

// callbacks for methods with `ref` / `out` parameters are possible but require some work (and Moq 4.8 or later):
delegate void SubmitCallback(ref Bar bar);

mock.Setup(foo => foo.Submit(ref It.Ref<Bar>.IsAny)
    .Callback(new SubmitCallback((ref Bar bar) => Console.WriteLine("Submitting a Bar!"));

2.6 Verification

Mock Verification

mock.Verify(foo => foo.DoSomething("ping"));

// Verify with custom error message for failure
mock.Verify(foo => foo.DoSomething("ping"), "When doing operation X, the service should be pinged always");

// Method should never be called
mock.Verify(foo => foo.DoSomething("ping"), Times.Never());

// Called at least once
mock.Verify(foo => foo.DoSomething("ping"), Times.AtLeastOnce());

// Verify getter invocation, regardless of value.
mock.VerifyGet(foo => foo.Name);

// Verify setter invocation, regardless of value.
mock.VerifySet(foo => foo.Name);

// Verify setter called with specific value
mock.VerifySet(foo => foo.Name ="foo");

// Verify setter with an argument matcher
mock.VerifySet(foo => foo.Value = It.IsInRange(1, 5, Range.Inclusive));

// Verify that no other invocations were made other than those already verified (requires Moq 4.8 or later)
mock.VerifyNoOtherCalls();

2.7 Customizing Mock Behavior

  • Make mock behave like a "true Mock", raising exceptions for anything that doesn't have a corresponding expectation: in Moq slang a "Strict" mock; default behavior is "Loose" mock, which never throws and returns default values or empty arrays, enumerables, etc. if no expectation is set for a member

    var mock = new Mock<IFoo>(MockBehavior.Strict);
    
  • Invoke base class implementation if no expectation overrides the member (a.k.a. "Partial Mocks" in Rhino Mocks): default is false. (this is required if you are mocking Web/Html controls in System.Web!)

    var mock = new Mock<IFoo> { CallBase = true };
    
  • Make an automatic recursive mock: a mock that will return a new mock for every member that doesn't have an expectation and whose return value can be mocked (i.e. it is not a value type)

    var mock = new Mock<IFoo> { DefaultValue = DefaultValue.Mock };
    // default is DefaultValue.Empty
    
    // this property access would return a new mock of Bar as it's "mock-able"
    Bar value = mock.Object.Bar;
    
    // the returned mock is reused, so further accesses to the property return 
    // the same mock instance. this allows us to also use this instance to 
    // set further expectations on it if we want
    var barMock = Mock.Get(value);
    barMock.Setup(b => b.Submit()).Returns(true);
    
  • Centralizing mock instance creation and management: you can create and verify all mocks in a single place by using a MockRepository, which allows setting the MockBehavior, its CallBase and DefaultValue consistently

    var repository = new MockRepository(MockBehavior.Strict) { DefaultValue = DefaultValue.Mock };
    
    // Create a mock using the repository settings
    var fooMock = repository.Create<IFoo>();
    
    // Create a mock overriding the repository settings
    var barMock = repository.Create<Bar>(MockBehavior.Loose);
    
    // Verify all verifiable expectations on all mocks created through the repository
    repository.Verify();
    

2.8 Miscellaneous

  • Setting up a member to return different values / throw exceptions on sequential calls:

    var mock = new Mock<IFoo>();
    mock.SetupSequence(f => f.GetCount())
        .Returns(3)  // will be returned on 1st invocation
        .Returns(2)  // will be returned on 2nd invocation
        .Returns(1)  // will be returned on 3rd invocation
        .Returns(0)  // will be returned on 4th invocation
        .Throws(new InvalidOperationException());  // will be thrown on 5th invocation
    
  • Setting expectations for protected members (you can't get IntelliSense for these, so you access them using the member name as a string):

    // at the top of the test fixture
    using Moq.Protected;
    
    // in the test
    var mock = new Mock<CommandBase>();
    mock.Protected()
         .Setup<int>("Execute")
         .Returns(5);
    
    // if you need argument matching, you MUST use ItExpr rather than It
    // planning on improving this for vNext (see below for an alternative in Moq 4.8)
    mock.Protected()
        .Setup<bool>("Execute",
            ItExpr.IsAny<string>())
        .Returns(true);
    
  • Moq 4.8 and later allows you to set up protected members through a completely unrelated type that has the same members and thus provides the type information necessary for IntelliSense to work. You can also use this interface to set up protected generic methods and those having by-ref parameters:

    interface CommandBaseProtectedMembers
    {
        bool Execute(string arg);
    }
    
    mock.Protected().As<CommandBaseProtectedMembers>()
        .Setup(m => m.Execute(It.IsAny<string>()))  // will set up CommandBase.Execute
        .Returns(true);
    

2.9 Advanced Features

  • Common

    // get mock from a mocked instance
    IFoo foo = // get mock instance somehow
    var fooMock = Mock.Get(foo);
    fooMock.Setup(f => f.GetCount()).Returns(42);
    
    
    // implementing multiple interfaces in mock
    var mock = new Mock<IFoo>();
    var disposableFoo = mock.As<IDisposable>();
    // now the IFoo mock also implements IDisposable :)
    disposableFoo.Setup(disposable => disposable.Dispose());
    
    // implementing multiple interfaces in single mock
    var mock = new Mock<IFoo>();
    mock.Setup(foo => foo.Name).Returns("Fred");
    mock.As<IDisposable>().Setup(disposable => disposable.Dispose());
    
    
    // custom matchers
    mock.Setup(foo => foo.DoSomething(IsLarge())).Throws<ArgumentException>();
    ...
    public string IsLarge() 
    { 
      return Match.Create<string>(s => !String.IsNullOrEmpty(s) && s.Length > 100);
    }
    
  • Mocking internal types: Add either of the following custom attributes (typically in AssemblyInfo.cs) to the project containing the internal types — which one you need depends on whether your own project is strong-named or not:

    // This assembly is the default dynamic assembly generated by Castle DynamicProxy, 
    // used by Moq. If your assembly is strong-named, paste the following in a single line:
    [assembly:InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=00...cc7")]
    
    // Or, if your own assembly is not strong-named, omit the public key:
    [assembly:InternalsVisibleTo("DynamicProxyGenAssembly2")]
    
  • Starting in Moq 4.8, you can create your own custom default value generation strategies (besides DefaultValue.Empty and DefaultValue.Mock) by subclassing DefaultValueProvider, or, if you want some more convenience, LookupOrFallbackDefaultValueProvider:

    class MyEmptyDefaultValueProvider : LookupOrFallbackDefaultValueProvider
    {
        public MyEmptyDefaultValueProvider()
        {
            base.Register(typeof(string), (type, mock) => "?");
            base.Register(typeof(List<>), (type, mock) => Activator.CreateInstance(type));
        }
    }
    
    var mock = new Mock<IFoo> { DefaultValueProvider = new MyEmptyDefaultValueProvider() };
    var name = mock.Object.Name;  // => "?"
    

    Note: When you pass the mock for consumption, you must pass mock.Object, not mock itself.

2.10 LINQ to Mocks

Moq is the one and only mocking framework that allows specifying mock behavior via declarative specification queries. You can think of LINQ to Mocks as:

Keep that query form in mind when reading the specifications:

var services = Mock.Of<IServiceProvider>(sp =>
    sp.GetService(typeof(IRepository)) == Mock.Of<IRepository>(r => r.IsAuthenticated == true) &&
    sp.GetService(typeof(IAuthentication)) == Mock.Of<IAuthentication>(a => a.AuthenticationType == "OAuth"));

// Multiple setups on a single mock and its recursive mocks
ControllerContext context = Mock.Of<ControllerContext>(ctx =>
     ctx.HttpContext.User.Identity.Name == "kzu" &&
     ctx.HttpContext.Request.IsAuthenticated == true &&
     ctx.HttpContext.Request.Url == new Uri("http://moqthis.com") &&
     ctx.HttpContext.Response.ContentType == "application/xml");

// Setting up multiple chained mocks:
var context = Mock.Of<ControllerContext>(ctx =>
     ctx.HttpContext.Request.Url == new Uri("http://moqthis.me") &&
     ctx.HttpContext.Response.ContentType 

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近,決定在一個項目用tp5進行APP介面開發,用Python做數據分析,然後這就面臨一個問題:PHP和Python如何進行數據交互? 思路 我解決此問題的方法是利用了PHP的passthru函數來調用命令運行Python腳本。 實現 在PHP中調用命令運行Python腳本 1 2 3 4 5 6 ...
  • 在實際開發中,有時候為了及時處理請求和進行響應,我們可能會多任務同時執行,或者先處理主任務,也就是非同步調用,非同步調用的實現有很多,例如多線程、定時任務、消息隊列等, 這一章節,我們就來講講@Async非同步方法調用。 一、@Async使用演示 @Async是Spring內置註解,用來處理非同步任務,在S ...
  • 如何提高效率問題,往往同樣的功能,不一樣的代碼,出來的效率往往大不一樣。 ● 用單引號代替雙引號來包含字元串,這樣做會更快一些。因為PHP會在雙引號包圍的字元串中搜尋變數,單引號則不會,註意:只有echo能這麼做,它 是一種可以把多個字元串當作參數的“函數”(譯註:PHP手冊中說echo是語言結構, ...
  • php數組中有一些函數與數學相關的函數,大多都是以array開頭然後下劃線接一個數學上的英文單詞,如下: 1 array_diff() 2 array_diff_assoc() 3 array_intersect() 4 array_intersect_assoc() 5 array_sum() 6 ...
  • 依賴註入 當A類需要依賴於B類,也就是說需要在A類中實例化B類的對象來使用時候,如果B類中的功能發生改變,也會導致A類中使用B類的地方也要跟著修改,導致A類與B類高耦合。這個時候解決方式是,A類應該去依賴B類的介面,把具體的類的實例化交給外部。 就拿我們業務中常用的通知模塊來說。 1 2 3 4 5 ...
  • 1. 基礎數據類型補充 li = ["李嘉誠", "麻花藤", "⻩海峰", "劉嘉玲"] s = "_".join(li) print(s) li = "⻩花⼤閨⼥" s = "_".join(li) print(s) 列表: 迴圈刪除列表中的每⼀個元素 li = [11, 22, 33, 44] ...
  • Platform.runLater一些情況下沒有賦值到fx頁面上 採用task方式 ...
  • 以前寫過ASP.NET Core 2.x的REST API文章,今年再更新一下到3.0版本。 預備知識:ASP.NET Core 和 C# 以前寫過ASP.NET Core 2.x的REST API文章,今年再更新一下到3.0版本。 預備知識:ASP.NET Core 和 C# 工具:Visual ...
一周排行
    -Advertisement-
    Play Games
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...