[英]Setup Moq to increment value when method called
I've got a POCO which im saving, like this: 我有一个保存的POCO,像这样:
_myRepo.Save(somePoco);
_myRepo
is mocked, eg: _myRepo
被嘲笑,例如:
var myRepo = new Mock<IRepo>();
When that Save method is called, i want to set somePoco.Id
to 1. 当调用Save方法时,我想将
somePoco.Id
设置为1。
How do i do it? 我该怎么做?
I see there is a Callback
method on the .Setup
, but it doesn't pass through the POCO, eg: 我看到
.Setup
上有一个Callback
方法,但它没有通过POCO,例如:
myRepo.Setup(x => x.Save(It.IsAny<SomePoco>())
.Callback(x => // how do i get the poco?);
The parameters get passed to the Callback
method you just need to specify the types explicitly. 参数传递给
Callback
方法,您只需要明确指定类型。
So the following should work: 所以以下应该有效:
myRepo.Setup(x => x.Save(It.IsAny<SomePoco>()))
.Callback<SomePoco>(poco => { poco.Id = 1; });
See also the samples in the quick start : 另请参阅快速入门中的示例:
// access invocation arguments
mock.Setup(foo => foo.Execute(It.IsAny<string>()))
.Returns(true)
.Callback((string s) => calls.Add(s));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.