简体   繁体   English

mock.Setup(...).Returns(..); 不是 mocking

[英]mock.Setup(…).Returns(..); Not mocking

I am running a WCF application and need to add some unit tests.我正在运行 WCF 应用程序,需要添加一些单元测试。 Initially I tried JustMock, free version as this is the standard for the company I am at at the moment however the free version does not support system.linq.xml最初我尝试了 JustMock,免费版本,因为这是我目前所在公司的标准,但是免费版本不support system.linq.xml

So, I moved over to Moq and thought that this would work, however the code block below所以,我搬到了最小起订量,并认为这会起作用,但是下面的代码块

Mock<IVersionFilter> mock = new Mock<IVersionFilter>();
var message = CreateValidGetProposalListMessage();
var returnValue = XDocument.Parse(GenerateXmlString());
mock.Setup(VersionFilter => VersionFilter.ParseMessage(message)).Returns(returnValue);

Which should mock the result of ParseMessage(...) from this call应该从这个调用中模拟 ParseMessage(...) 的结果

public override bool Match(Message message)
{
     var doc = ParseMessage(message);

     var getProposalList = doc.Descendants(_xmlnsa + MethodConstants.GetProposalList).FirstOrDefault();
     if (getProposalList != null)
     {
        // code ommitted
     }
     /// Test code ommitted
}

public XDocument ParseMessage(Message message)
{
     XDocument doc = XDocument.Parse(message.ToString());
     return doc;
}

public Message CreateValidGetProposalListMessage()
{
    var p = new getProposalList
    {
        Code = "xxxx"
    };

    var message = Message.CreateMessage(MessageVersion.Soap11, "getProposalList xmlns=\"http://xxx\">", p);

    return message;
}

I cant see what I have missed and would be grateful if someone could help me progress this issue forward.我看不出我错过了什么,如果有人能帮助我推进这个问题,我将不胜感激。

Thanks谢谢

The setup looks mostly ok.设置看起来基本没问题。 I suspect your problem is that setup isn't being invoked at all because of argument equality.我怀疑您的问题是由于参数相等,根本没有调用设置。

mock.Setup(VersionFilter => VersionFilter.ParseMessage(message)).Returns(returnValue);

This is saying that ParseMessage invoked with something that equals message will return returnValue .这就是说ParseMessage用等于message的东西调用将返回returnValue If Message is a class this will only work if Message implements it's own Equals method.如果Message是 class 这只有在Message实现它自己的Equals方法时才有效。

As mentioned in my OP comment, start with the basics.正如我的 OP 评论中提到的,从基础开始。 Ensure that the setup is being invoked first:确保首先调用设置:

mock.Setup(VersionFilter => VersionFilter.ParseMessage(It.IsAny<Message>())).Returns(returnValue);

Once you've got that working, tune it to suit the test case.一旦你得到这个工作,调整它以适应测试用例。 You can either implement an Equals method for the Message type (Fody would be my suggestion if you want to this), or match it another way via It.Is<Message>(message => match conditions) .您可以为Message类型实现Equals方法(如果您愿意,我建议使用 Fody),或者通过It.Is<Message>(message => match conditions)以另一种方式匹配它。

You have created a mock of IVersionFilter with this line您已使用此行创建了IVersionFilter的模拟

Mock<IVersionFilter> mock = new Mock<IVersionFilter>();

But I would expect to see a class that takes a IVersionFilter in its constructor.但我希望看到一个IVersionFilter在其构造函数中采用 IVersionFilter。 You would then do然后你会做

 var myTestClass = new TestClass(mock.Object)

Then when the test class uses the functionality in IVersionFilter it uses the mocked one然后,当测试IVersionFilter使用 IVersionFilter 中的功能时,它使用模拟的功能

Just to clarify, TestClass would be something like澄清一下,TestClass 类似于

public class TestClass(IVersionFilter filter)
{
     public void DoSomething()
     {
         filter.DoSomething();
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM