C# MOQ 文档在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/231175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:07:48  来源:igfitidea点击:

Where is the MOQ documentation?

c#.nettestingmockingmoq

提问by Jeremy Holt

Where can I find comprehensive documentation for MOQ? I'm just starting with mocking and am having difficulty getting my head around it. I've read through all the links at http://code.google.com/p/moq/wiki/QuickStartbut can't seem to find a tutorial or gentle introduction.

在哪里可以找到 MOQ 的综合文档?我只是从嘲笑开始,我很难理解它。我已经通读了http://code.google.com/p/moq/wiki/QuickStart 上的所有链接,但似乎找不到教程或简单的介绍。

I have also looked briefly at Rhino Mocks but found it very confusing.

我还简要地查看了 Rhino Mocks,但发现它非常令人困惑。



Yes - I read Stephen Walthers article - very helpful. I also went through the links. I can't seem to watch the video at http://www.bestechvideos.com/2008/06/08/dimecasts-net-introduction-to-mocking-with-moq[broken link]

是的 - 我读过 Stephen Walthers 的文章 - 很有帮助。我也浏览了链接。我似乎无法在http://www.bestechvideos.com/2008/06/08/dimecasts-net-introduction-to-mocking-with-moq[断开的链接]上观看视频

Specifically I am trying to determine whether an event was raised from the mocked class. I can't get the example for events on the QuickStarts page to compile. On the google groups, Daniel explained that CreateEventHandler can only handle an event of type EventHandler<TEventArgs>, but even then I can't get the code to compile.

具体来说,我试图确定是否从模拟类中引发了一个事件。我无法在 QuickStarts 页面上获取要编译的事件示例。在 google 组上,Daniel 解释说 CreateEventHandler 只能处理类型为 的事件EventHandler<TEventArgs>,但即使如此我也无法编译代码。

More specifically I have a class that implements INotifyChanged.

更具体地说,我有一个实现INotifyChanged.

public class Entity : INotifyChanged
{
    public event PropertyChangingEventHandler PropertyChanging;

    public int Id 
      { 
          get {return _id;}
          set {
                 _id = value;
                 OnPropertyChanged("Id");
              }
      }

     protected void OnPropertyChanged(string property)
      {
         if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
 etc .....    
}

How do I mock the class to test whether the PropertyChangedevent was fired? I can't rewrite the event to public event EventHandler<PropertyChangedEventArgs>becuase I get this error:

如何模拟课程以测试PropertyChanged事件是否被触发?我无法重写事件,public event EventHandler<PropertyChangedEventArgs>因为我收到此错误:

Error 1 'CoreServices.Notifier' does not implement interface member System.ComponentModel.INotifyPropertyChanged.PropertyChanged'. 'CoreServices.Notifier.PropertyChanged' cannot implement 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged' because it does not have the matching return type of 'System.ComponentModel.PropertyChangedEventHandler'.

错误 1 ​​'CoreServices.Notifier' 未实现接口成员 System.ComponentModel.INotifyPropertyChanged.PropertyChanged'。“CoreServices.Notifier.PropertyChanged”无法实现“System.ComponentModel.INotifyPropertyChanged.PropertyChanged”,因为它没有“System.ComponentModel.PropertyChangedEventHandler”的匹配返回类型。

回答by Bill the Lizard

Have you watched Introduction to Mocking with Moq? It's an introductory overview of using Moq and is intended for those who are new to either mocking in general, or the Moq framework itself.

您是否看过使用 Moq 进行 Mocking 的介绍?这是使用 Moq 的介绍性概述,适用于那些不熟悉一般模拟或 Moq 框架本身的人。

回答by Brian J Cardiff

Have you read the linked pages at https://github.com/Moq/moq4/wiki/Quickstart? for example this one(probably moved to stephen walthers personal blog)

您是否阅读过https://github.com/Moq/moq4/wiki/Quickstart 上的链接页面?例如这个(可能搬到了斯蒂芬沃尔瑟的个人博客

回答by Dylan Beattie

Moq's latest documentation is now available at github wiki page:

Moq 的最新文档现在可以在 github wiki 页面上找到:

https://github.com/Moq/moq4/wiki/Quickstart

https://github.com/Moq/moq4/wiki/Quickstart

Previously they were on Google Code. As well as the wiki and other online resources, there's full documentation in Windows .CHM help-file format included in the Moq binary downloadlinked from the Moq homepage.

以前他们在谷歌代码上。除了 wiki 和其他在线资源,Moq 主页上链接的Moq 二进制下载中还包含 Windows .CHM 帮助文件格式的完整文档。

回答by TrueWill

I am trying to determine whether an event was raised from the mocked class.

我试图确定是否从模拟类中引发了一个事件。

Are you? Or are you trying to determine if the Idproperty was set? Remember, by default a mock has no behavior. It's not raising notification events.

你是?或者您是否试图确定该Id属性是否已设置?请记住,默认情况下模拟没有行为。它不会引发通知事件。

I'd do:

我会做:

const int ExpectedId = 123;
mockEntity.VerifySet(x => x.Id = ExpectedId);

This assumes that Entity implements an interface; one example:

这假设 Entity 实现了一个接口;一个例子:

public interface IKeyedEntity
{
    int Id { get; set; }
}

That said, if Entityis a POCOwith no interesting behavior I would neither implement an interface (other than INotifyChanged) nor mock it. Test with an actual Entityinstance (just don't use a database). Reserve mocking for services and complex dependencies.

也就是说,如果Entity是一个没有有趣行为的POCO,我既不会实现接口(除了INotifyChanged),也不会模拟它。使用实际Entity实例进行测试(只是不要使用数据库)。保留对服务和复杂依赖项的模拟。

For more Moq features, see

有关更多起订量功能,请参阅

Old style imperative mocks vs moq functional specificationsand Mock.Of - how to specify behavior? (thread). I also posted my own example of Moq v4 functional specifications.

旧式命令式模拟vs moq 功能规范Mock.Of - 如何指定行为?(线程)。我还发布了我自己的Moq v4 功能规范示例。