- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
既然已经找到了解决方案,那么简短的总结:
AutoFixture 返回卡住模拟就好了;我的 sut 也是由 AutoFixture 生成的,它只有一个公共(public)属性,其中包含对测试很重要的本地默认值,并且 AutoFixture 设置为新值。从 Mark 的回答中可以学到很多东西。
原始问题:
我昨天开始尝试使用 AutoFixture 进行我的 xUnit.net 测试,这些测试都具有最小起订量。我希望替换一些最小起订量的东西或使其更易于阅读,而且我对在 SUT 工厂容量中使用 AutoFixture 特别感兴趣。
我用 Mark Seemann 的一些关于 AutoMocking 的博客文章武装自己,并尝试从那里开始工作,但我并没有走得太远。
这是我在没有 AutoFixture 时的测试结果:
[Fact]
public void GetXml_ReturnsCorrectXElement()
{
// Arrange
string xmlString = @"
<mappings>
<mapping source='gcnm_loan_amount_min' target='gcnm_loan_amount_min_usd' />
<mapping source='gcnm_loan_amount_max' target='gcnm_loan_amount_max_usd' />
</mappings>";
string settingKey = "gcCreditApplicationUsdFieldMappings";
Mock<ISettings> settingsMock = new Mock<ISettings>();
settingsMock.Setup(s => s.Get(settingKey)).Returns(xmlString);
ISettings settings = settingsMock.Object;
ITracingService tracing = new Mock<ITracingService>().Object;
XElement expectedXml = XElement.Parse(xmlString);
IMappingXml sut = new SettingMappingXml(settings, tracing);
// Act
XElement actualXml = sut.GetXml();
// Assert
Assert.True(XNode.DeepEquals(expectedXml, actualXml));
}
这里的故事很简单 - 确保 SettingMappingXml
查询 ISettings
具有正确 key (硬编码/属性注入(inject))的依赖项并将结果作为 XElement
返回. ITracingService
仅当出现错误时才相关。
我试图做的是摆脱显式创建 ITracingService
的需要对象,然后手动注入(inject)依赖项(不是因为这个测试太复杂,而是因为它足够简单,可以尝试并理解它们)。
输入 AutoFixture - 第一次尝试:
[Fact]
public void GetXml_ReturnsCorrectXElement()
{
// Arrange
IFixture fixture = new Fixture();
fixture.Customize(new AutoMoqCustomization());
string xmlString = @"
<mappings>
<mapping source='gcnm_loan_amount_min' target='gcnm_loan_amount_min_usd' />
<mapping source='gcnm_loan_amount_max' target='gcnm_loan_amount_max_usd' />
</mappings>";
string settingKey = "gcCreditApplicationUsdFieldMappings";
Mock<ISettings> settingsMock = new Mock<ISettings>();
settingsMock.Setup(s => s.Get(settingKey)).Returns(xmlString);
ISettings settings = settingsMock.Object;
fixture.Inject(settings);
XElement expectedXml = XElement.Parse(xmlString);
IMappingXml sut = fixture.CreateAnonymous<SettingMappingXml>();
// Act
XElement actualXml = sut.GetXml();
// Assert
Assert.True(XNode.DeepEquals(expectedXml, actualXml));
}
我希望 CreateAnonymous<SettingMappingXml>()
, 在检测到 ISettings
构造函数参数,以注意已为该接口(interface)注册了一个具体实例并注入(inject)它 - 然而,它并没有这样做,而是创建了一个新的匿名实现。
这特别令人困惑,因为 fixture.CreateAnonymous<ISettings>()
确实返回我的实例 -
IMappingXml sut = new SettingMappingXml(fixture.CreateAnonymous<ISettings>(), fixture.CreateAnonymous<ITracingService>());
使测试完全绿色,这行正是我在实例化 SettingMappingXml
时期望 AutoFixture 在内部执行的操作.
然后是卡住组件的概念,所以我继续卡住夹具中的模拟而不是获取模拟对象:
fixture.Freeze<Mock<ISettings>>(f => f.Do(m => m.Setup(s => s.Get(settingKey)).Returns(xmlString)));
果然这工作得很好——只要我调用 SettingMappingXml
显式构造函数并且不依赖 CreateAnonymous()
.
简而言之,我不明白为什么它会以它明显的方式工作,因为它违背了我能想到的任何逻辑。通常我会怀疑库中的错误,但这是非常基本的事情,我相信其他人会遇到这个问题,而且它早就被发现并修复了。更重要的是,了解 Mark 对测试和 DI 的孜孜不倦的态度,这不是无意的。
这反过来意味着我一定遗漏了一些相当基本的东西。我怎样才能让我的 SUT 由 AutoFixture 创建,并将预配置的模拟对象作为依赖项?我现在唯一确定的是我需要 AutoMoqCustomization
所以我不必为 ITracingService
配置任何东西.
AutoFixture/AutoMoq 包是 2.14.1,Moq 是 3.1.416.3,全部来自 NuGet。 .NET 版本为 4.5(与 VS2012 一起安装),VS2012 和 2010 中的行为相同。
在写这篇文章时,我发现有些人遇到了 Moq 4.0 和程序集绑定(bind)重定向的问题,所以我小心翼翼地清除了我的解决方案中的任何 Moq 4 实例,并通过将 AutoFixture.AutoMoq 安装到“clean”中来安装 Moq 3.1项目。但是,我的测试行为保持不变。
感谢您的任何指点和解释。
更新:这是 Mark 要求的构造函数代码:
public SettingMappingXml(ISettings settingSource, ITracingService tracing)
{
this._settingSource = settingSource;
this._tracing = tracing;
this.SettingKey = "gcCreditApplicationUsdFieldMappings";
}
为了完整起见,GetXml()
方法如下所示:
public XElement GetXml()
{
int errorCode = 10600;
try
{
string mappingSetting = this._settingSource.Get(this.SettingKey);
errorCode++;
XElement mappingXml = XElement.Parse(mappingSetting);
errorCode++;
return mappingXml;
}
catch (Exception e)
{
this._tracing.Trace(errorCode, e.Message);
throw;
}
}
SettingKey
只是一个自动属性。
最佳答案
假设 SettingKey
属性定义如下,我现在可以重现问题:
public string SettingKey { get; set; }
发生的事情是 Test Doubles注入(inject)到 SettingMappingXml 实例中非常好,但是因为 SettingKey
是可写的,AutoFixture 的自动属性功能会启动并修改值。
考虑这段代码:
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var sut = fixture.CreateAnonymous<SettingMappingXml>();
Console.WriteLine(sut.SettingKey);
这会打印出如下内容:
SettingKey83b75965-2886-4308-bcc4-eb0f8e63de09
即使所有测试替身都已正确注入(inject),Setup
方法中的预期仍未满足。
有很多方法可以解决这个问题。
保护不变量
解决此问题的正确方法是使用单元测试和 AutoFixture 作为反馈机制。这是GOOS中的关键点之一。 : 单元测试的问题通常是设计缺陷的症状,而不是单元测试(或 AutoFixture)本身的错误。
在这种情况下,它向我表明 the design isn't fool-proof enough .客户端可以随意操作 SettingKey
真的合适吗?
至少,我会推荐这样的替代实现:
public string SettingKey { get; private set; }
有了这个改变,我的重现就通过了。
省略设置键
如果您不能(或不愿)更改您的设计,您可以指示 AutoFixture 跳过设置 SettingKey
属性:
IMappingXml sut = fixture
.Build<SettingMappingXml>()
.Without(s => s.SettingKey)
.CreateAnonymous();
就个人而言,我发现每次需要特定类的实例时都必须编写 Build
表达式会适得其反。您可以将 SettingMappingXml
实例的创建方式与实际实例化分离:
fixture.Customize<SettingMappingXml>(
c => c.Without(s => s.SettingKey));
IMappingXml sut = fixture.CreateAnonymous<SettingMappingXml>();
为了更进一步,您可以将 Customize
方法调用封装在 a Customization 中.
public class SettingMappingXmlCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<SettingMappingXml>(
c => c.Without(s => s.SettingKey));
}
}
这需要您使用该自定义创建您的 Fixture
实例:
IFixture fixture = new Fixture()
.Customize(new SettingMappingXmlCustomization())
.Customize(new AutoMoqCustomization());
一旦您获得超过两个或三个要链接的自定义,您可能会厌倦一直编写该方法链。是时候将这些定制封装到您的特定库的一组约定中了:
public class TestConventions : CompositeCustomization
{
public TestConventions()
: base(
new SettingMappingXmlCustomization(),
new AutoMoqCustomization())
{
}
}
这使您始终可以像这样创建 Fixture
实例:
IFixture fixture = new Fixture().Customize(new TestConventions());
TestConventions
为您提供了一个中心位置,您可以在需要时偶尔修改测试套件的约定。它减少了单元测试的可维护性负担,并有助于使生产代码的设计更加一致。
最后,由于看起来您正在使用 xUnit.net,因此您可以利用 AutoFixture's xUnit.net integration ,但在执行此操作之前,您需要使用命令式较少的方式来操作 Fixture
。事实证明,创建、配置和注入(inject) ISettings
Test Double 的代码非常地道,它有一个名为 Freeze 的快捷方式。 :
fixture.Freeze<Mock<ISettings>>()
.Setup(s => s.Get(settingKey)).Returns(xmlString);
有了这些,下一步就是定义一个自定义的 AutoDataAttribute:
public class AutoConventionDataAttribute : AutoDataAttribute
{
public AutoConventionDataAttribute()
: base(new Fixture().Customize(new TestConventions()))
{
}
}
您现在可以将测试简化为最基本的要素,去除所有噪音,使测试能够简洁地仅表达重要的内容:
[Theory, AutoConventionData]
public void ReducedTheory(
[Frozen]Mock<ISettings> settingsStub,
SettingMappingXml sut)
{
string xmlString = @"
<mappings>
<mapping source='gcnm_loan_amount_min' target='gcnm_loan_amount_min_usd' />
<mapping source='gcnm_loan_amount_max' target='gcnm_loan_amount_max_usd' />
</mappings>";
string settingKey = "gcCreditApplicationUsdFieldMappings";
settingsStub.Setup(s => s.Get(settingKey)).Returns(xmlString);
XElement actualXml = sut.GetXml();
XElement expectedXml = XElement.Parse(xmlString);
Assert.True(XNode.DeepEquals(expectedXml, actualXml));
}
其他选项
要使原始测试通过,您也可以完全关闭自动属性:
fixture.OmitAutoProperties = true;
关于c# - AutoFixture/AutoMoq 忽略注入(inject)实例/卡住模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13484471/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!