[英]How to add unit tests using Moq and xUnit in this specific scenario?
I have this method in my data/service layer. 我的数据/服务层中有此方法。 We are commencing the usage of xUnit and MoQ for unit testing.
我们开始将xUnit和MoQ用于单元测试。 We use Autofac as our IoC container.
我们使用Autofac作为IoC容器。
How to set up a unit test for this method using xUnit and MoQ ? 如何使用xUnit和MoQ为此方法设置单元测试? Request specific code illustration with usage of Fake (if needed)
请求使用Fake的特定代码插图(如果需要)
public async Task<AddressModel> AddAddress(string useridKey, AddressModel addressModel)
{
IAddress address = AddressConverters.ToIAddressFromAddressModel(addressModel);
IAddress newAddress = await _userManager.AddShippingAddressAsync(userKey, address).ConfigureAwait(false);
return AddressConverters.ToAddressModel(newAddress);
}
You need to inject a mock user manager then do something like 您需要注入一个模拟用户管理器,然后执行类似的操作
mockUserManager.Setup(arg=>arg.AddShippingAddressAsync(It.Is<string>, It.Is<IAddress>))
.ReturnsAsync(myNewAddressWhateverYouWantToReturn);
I'm not 100% about xUnit async support, but it looks like it should be something like 我对xUnit async的支持不是100%,但是看起来应该像
[Fact]
private async Task TestAddingAddress()
{
// inject your mock, either here on setup...
var thingWithAdd = new Foo(); //or inject mock into constructor...
var newAddress = await thingWithAdd.AddAddress("", new AddressModel());
// assert something about it....
}
However, I have no idea what some of your functions do, or what dependencies they have, you may need to mock some more things. 但是,我不知道您的某些功能做什么,或者它们具有什么依赖关系,您可能需要模拟更多的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.