简体   繁体   English

Moq对于不同的IEnumerable参数设置返回相同的结果

[英]Moq returns the same result for different IEnumerable parameter setups

I have come across a very strange behavior in moq and I cannot understand whether it's a bug or I am doing something wrong. 我在最小起订量中遇到了一个非常奇怪的行为,我无法理解这是一个错误还是做错了什么。 Here is the example: 这是示例:

List<CustomerDataTransaction> transactions0 = GetTransactionsSomehow();
List<CustomerDataTransaction> transactions1 = GetTransactionsSomehow();

var portfolioTransactions0 = new List<IPortfolioTransaction>();
var portfolioTransactions1 = new List<IPortfolioTransaction>();

m_TransactionMapperMock
    .Setup(m => m.CreatePortfolioTransactions(transactions0))
    .Returns(portfolioTransactions0);

m_TransactionMapperMock
    .Setup(m => m.CreatePortfolioTransactions(transactions1))
    .Returns(portfolioTransactions1);

I have checked that transaction0 is not equal to transactions1, so it's for sure different lists. 我已经检查了transaction0不等于transactions1,因此可以肯定是不同的列表。 But the mock returns portfolioTransactions1 twice when called with different parameters (transactions0 and transactions1). 但是,当使用不同的参数(transactions0和transactions1)调用时,该模拟两次返回PortfolioTransactions1。 I tried to figure out what was wrong, but I did not find any reasons. 我试图找出问题所在,但没有发现任何原因。 Then I have added dummy elements to each of the lists and that has fixed it, mock started to return different values as planned. 然后,我向每个列表中添加了虚拟元素,并对其进行了修复,模拟开始按计划返回不同的值。 Is this a bug of Mock or I don't get something? 这是Mock的错误,还是我什么都没得到? As far as I understand the values inside the list should not affect it at all. 据我了解,列表中的值根本不会影响它。

PS I don't know if it's important or not but the method accepts IEnumerable<CustomerDataTransaction> PS我不知道它是否重要,但是该方法接受IEnumerable<CustomerDataTransaction>

Consider trying: 考虑尝试:

It.Is<IEnumerable<CustomerDataTransaction>>(t => t == transactions1))

From memory I thought Moq used reference equality 从记忆中,我认为Moq使用了引用相等

Expanding on NinjaNye's answer, using It.Is satisfies the test is because it ensures that you are looking at the exact instance of the object, not the equality operator. 使用It扩展NinjaNye的答案是令人满意的,因为它可以确保您查看的是对象的确切实例,而不是相等运算符。

With your testing framework, if you say Assert.NotEqual and give it two objects, it uses the equality operator and says one empty list equal to the other. 在您的测试框架中,如果您说Assert.NotEqual并给它两个对象,它将使用相等运算符并说一个空列表等于另一个。 Think about being able to compare objectA.Property1 to objectA.Property2. 考虑一下能够比较objectA.Property1和objectA.Property2。 It makes sense that Assert.Equal would look at the values, not the instance of the value. Assert.Equal将查看值而不是值的实例是有意义的。

Xunit provides a method Assert.Same, which will provide the same functionality as Moq's It.Is<>, verifying it is the same instance of the object. Xunit提供了一个Assert.Same方法,该方法将提供与Moq的It.Is <>相同的功能,并验证它是该对象的相同实例。 I believe that most of the popular testing frameworks today provide similar behavior. 我相信当今大多数流行的测试框架都提供类似的行为。

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

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