为什么最小起订量不在其设置中使用此参数?
我有这个测试
[Fact]
public void Get_if_item_is_not_in_cache_return_null_returns_true()
{
var repo = new Repository<IProduct>(
this.factoryMock.Object,
this.cacheMock.Object,
this.readerMock.Object,
this.storageMock.Object);
var key = 1;
const string Name = "Product1";
var date = new DateTime(0, DateTimeKind.Utc);
var product1 = this.Product; /* returns new Product(
* "Product1",
* new DateTime(0, DateTimeKind.Utc),
* new Dictionary<string, decimal>
* { { "@1lom", 0m }, { "@2lom", 0m } }) */
this.cacheMock.Setup(
m => m.Add(key, product1)).Returns(product1);
this.cacheMock.Setup(
m => m.Get<IList<IDictionary<string, object>>>(0)).Returns(null as IList<IDictionary<string, object>>);
this.cacheMock.Setup(
m => m.Get<IProduct>(key)).Returns(null as IProduct);
this.factoryMock.Setup(
m => m.Create(
Name,
date,
this.cacheMock.Object.Get<IList<IDictionary<string, object>>>(0))).Returns(product1);
var product2 = repo.Get(key, Name, date);
Assert.Null(product2);
this.cacheMock.VerifyAll();
this.factoryMock.VerifyAll();
}
我得到这个例外
<块引用>Moq.MockVerificationException:以下设置不匹配:
ICache m => m.Add(1, )
调用包含第二个参数,但为什么 moq 在设置过程中无法识别它?当我省略 .Add!? 的设置时,它有效。
更新 这是执行的代码
public virtual T Get(int key, string productName, DateTime date)
{
return this.Cache.Get<T>(key) ?? this.PersistenStorage.Query(productName, date) ?? this.CreateNewCacheItem(productName, date);
}
protected virtual T CreateNewCacheItem(string productName, DateTime date)
{
var product = this.Factory.Create(productName, date, this.Cache.Get<IList<IDictionary<string, object>>>(this.RawDataKey));
return this.Cache.Add(product.GetHashCode(), product);
}
I have this test
[Fact]
public void Get_if_item_is_not_in_cache_return_null_returns_true()
{
var repo = new Repository<IProduct>(
this.factoryMock.Object,
this.cacheMock.Object,
this.readerMock.Object,
this.storageMock.Object);
var key = 1;
const string Name = "Product1";
var date = new DateTime(0, DateTimeKind.Utc);
var product1 = this.Product; /* returns new Product(
* "Product1",
* new DateTime(0, DateTimeKind.Utc),
* new Dictionary<string, decimal>
* { { "@1lom", 0m }, { "@2lom", 0m } }) */
this.cacheMock.Setup(
m => m.Add(key, product1)).Returns(product1);
this.cacheMock.Setup(
m => m.Get<IList<IDictionary<string, object>>>(0)).Returns(null as IList<IDictionary<string, object>>);
this.cacheMock.Setup(
m => m.Get<IProduct>(key)).Returns(null as IProduct);
this.factoryMock.Setup(
m => m.Create(
Name,
date,
this.cacheMock.Object.Get<IList<IDictionary<string, object>>>(0))).Returns(product1);
var product2 = repo.Get(key, Name, date);
Assert.Null(product2);
this.cacheMock.VerifyAll();
this.factoryMock.VerifyAll();
}
I get this exception
Moq.MockVerificationException: The following setups were not matched:
ICache m => m.Add(1, )
The invocation contains the second parameter but why does moq not recognize it during setup? It works, when i omit the setup for .Add!?
Update
This is the code that is executed
public virtual T Get(int key, string productName, DateTime date)
{
return this.Cache.Get<T>(key) ?? this.PersistenStorage.Query(productName, date) ?? this.CreateNewCacheItem(productName, date);
}
protected virtual T CreateNewCacheItem(string productName, DateTime date)
{
var product = this.Factory.Create(productName, date, this.Cache.Get<IList<IDictionary<string, object>>>(this.RawDataKey));
return this.Cache.Add(product.GetHashCode(), product);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在起订量中使用设置时,您会说“当您确切地看到这些参数时返回此”。对于像
Product
这样的引用类型,它只有在看到确切的实例时才会起作用。因此,当 Get 运行时,它会在内部创建一个新的Product
实例,并且与您对product1
的期望不符。正如 Chris 提到的,如果您在设置中使用
It.IsAny()
,它将匹配新实例并按预期工作。When you use Setup in Moq, you're saying "Return this when you see exactly these parameters". For reference types like
Product
, it will only work when it sees that exact instance. So when Get runs, it's creating a newProduct
instance internally and is not matching your expectation fromproduct1
.As Chris mentioned, if you used
It.IsAny<Product>()
in the Setup instead, it would match the new instance and work as intended.