单元测试静态实用程序 class
Unit testing static utility class
如何在静态 class 中对静态方法进行单元测试?
有这个代码:
public class AddressConverter {
public static BillingAddress ConvertAddress(ShippingAddress address)
{
var billingAddress = new BillingAddress (); // this is the problem - 3rd party lib
...
}
我正在使用第 3 方库,我唯一可以修改的是 AddressConverter class。顺便说一下,BillingAddress() 是一个第三方库,
反编译时显示:
// decompiled code
public class BillingAddress : IOrderAddress
{
public BillingAddress() : base(SomeSessionContext.Current.Class)
{
...
问题是我无法创建 new BillingAddress()
因为它的值取自某些会话变量等
我该如何测试?有什么解决方法吗?
不要在 AddressConverter 中创建 BillingAddress。为 ConvertAddress 使用额外的参数或删除静态关键字并使用 BillingAddressFactory 创建 IOrderAddress 的新实例。
如果您无法重构以将 BillingAddress 注入到静态方法中,您可以使用 Microsoft Fakes 对此进行测试。
基本上你会为你的第 3 方 DLL 添加一个 Fakes 库:
In Solution Explorer, open your unit test project’s references and
select the reference to the assembly that contains the method you want
to fake. ... Choose Add Fakes Assembly.
那你应该可以用ShimBillingAddress
了。 (航空代码警告,我无权访问您的第 3 方库 :-))
using (ShimsContext.Create())
{
// Arrange:
YourThirdPartyLib.Fakes.ShimBillingAddress.SomeMethod = () => { return "some meaningful value"; };
// Instantiate the component under test:
var sut = new AddressConverter();
// Act:
var result = sut.ConvertAddress(someShippingAddress);
// Assert:
}
摘自 MSDN - Isolating Code Under Test with Microsoft Fakes // Getting started with shims and MSDN - Using shims to isolate your application from other assemblies for unit testing 的引述和说明。
MSDN 上有关于 naming conventions 伪造垫片的信息,因为它并不总是很明显。
此外, 的后半部分有一个关于为系统 dll 设置伪造的演练。
如何在静态 class 中对静态方法进行单元测试?
有这个代码:
public class AddressConverter {
public static BillingAddress ConvertAddress(ShippingAddress address)
{
var billingAddress = new BillingAddress (); // this is the problem - 3rd party lib
...
}
我正在使用第 3 方库,我唯一可以修改的是 AddressConverter class。顺便说一下,BillingAddress() 是一个第三方库, 反编译时显示:
// decompiled code
public class BillingAddress : IOrderAddress
{
public BillingAddress() : base(SomeSessionContext.Current.Class)
{
...
问题是我无法创建 new BillingAddress()
因为它的值取自某些会话变量等
我该如何测试?有什么解决方法吗?
不要在 AddressConverter 中创建 BillingAddress。为 ConvertAddress 使用额外的参数或删除静态关键字并使用 BillingAddressFactory 创建 IOrderAddress 的新实例。
如果您无法重构以将 BillingAddress 注入到静态方法中,您可以使用 Microsoft Fakes 对此进行测试。
基本上你会为你的第 3 方 DLL 添加一个 Fakes 库:
In Solution Explorer, open your unit test project’s references and select the reference to the assembly that contains the method you want to fake. ... Choose Add Fakes Assembly.
那你应该可以用ShimBillingAddress
了。 (航空代码警告,我无权访问您的第 3 方库 :-))
using (ShimsContext.Create())
{
// Arrange:
YourThirdPartyLib.Fakes.ShimBillingAddress.SomeMethod = () => { return "some meaningful value"; };
// Instantiate the component under test:
var sut = new AddressConverter();
// Act:
var result = sut.ConvertAddress(someShippingAddress);
// Assert:
}
摘自 MSDN - Isolating Code Under Test with Microsoft Fakes // Getting started with shims and MSDN - Using shims to isolate your application from other assemblies for unit testing 的引述和说明。
MSDN 上有关于 naming conventions 伪造垫片的信息,因为它并不总是很明显。
此外,