- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我将 AutoFixture 与 AutoMoqCustomization
一起使用并尝试创建一个包含只读属性的类的实例:
public override ILog Logger { get; } = LogManager.GetLogger(typeof(MyService));
想法是我应该能够卡住我的测试 ILog
测试双重使用:
var log = fixture.Freeze<Mock<ILog>>;
并通过以下方式验证它在主方法调用之后被调用:
log.Verify(l => l.Warn, Times.Once);
然而,当我调用 fixture.Create<MyService>
AutoFixture 不会替换 Logger
带有模拟 ILog
的属性.我还尝试删除默认值 LogManager.GetLogger<etc>
在这种情况下 ILog
的值是null
.
其他属性正确填充了测试替身,但这个不是。
作为引用,ILog
接口(interface)来自 ServiceStack 的日志框架,看起来像这样:
public interface ILog
{
bool IsDebugEnabled { get; }
void Debug(object message);
void Debug(object message, Exception exception);
void DebugFormat(string format, params object[] args);
void Error(object message);
void Error(object message, Exception exception);
void ErrorFormat(string format, params object[] args);
void Fatal(object message);
void Fatal(object message, Exception exception);
void FatalFormat(string format, params object[] args);
void Info(object message);
void Info(object message, Exception exception);
void InfoFormat(string format, params object[] args);
void Warn(object message);
void Warn(object message, Exception exception);
void WarnFormat(string format, params object[] args);
}
我还验证了手动创建 Mock 并使用 Moq 进行设置是可行的 - ILog
属性已正确替换为我的 Mock 使用:
myServiceMock.Setup(s => s.Logger).Returns(myLoggerMock)
任何人都可以阐明这一点吗?
重现步骤
测试
using ServiceStack;
using ServiceStack.Logging;
using ServiceStack.Web;
using MyApp;
[Test]
public void LogTest()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var log = fixture.Freeze<Mock<ILog>>();
var request = fixture.Freeze<Mock<IRequest>>();
var response = new Mock<IResponse>();
var service = fixture.Create<MyService>();
request.Setup(r => r.Response).Returns(response.Object);
service.Post(null);
log.Verify(l => l.Warn(It.IsAny<string>()), Times.Once());
}
服务等级 - 注意:通常是 Logger
属性将以 = LogManager.GetLogger(typeof(MyService))
为后缀但我在这一点上省略了它以查看问题。
using ServiceStack;
using ServiceStack.Logging;
namespace MyApp
{
public class MyService : BaseService
{
public override ILog Logger { get; }
public MyResponse Post(MyRequest request)
{
if (request != null) return new MyResponse() {Message = request.Message};
Logger.Warn("Null request object");
return null;
}
}
public abstract class BaseService : Service
{
public abstract ILog Logger { get; }
}
public class MyRequest
{
public string Message { get; set; }
}
public class MyResponse
{
public string Message { get; set; }
}
}
如果你在 service.Post(null)
上设置断点行你会看到 ILog
属性仍然为空,但其他属性有模拟。
最佳答案
所有 AutoMoqCustomization
所做的就是配置 AutoFixture 以将对接口(interface)或抽象类型的所有请求委托(delegate)给 Moq。它不会自动为创建的测试替身的属性和方法设置任何 stub 。
但是,截至AutoFixture 3.20.0有a new auto-mocking customization正是这样做的——AutoConfiguredMoqCustomization
:
var fixture = new Fixture().Customize(new AutoConfiguredMoqCustomization());
启用该自定义可确保将来自 Moq 的测试替身配置为返回由 AutoFixture 从其所有公共(public)属性创建的对象。
但是有一个问题。作为reported by @dcastro , 错误是 introduced in Moq 4.2.1502.911这会导致只读属性——比如你的情况下的MyService.Logger
属性——在AutoFixture配置它们之后被Moq覆盖。
因此,即使您切换到 AutoConfiguredMoqCustomization
,Logger
属性仍仍为 null,因为它是只读的。另一方面,其他具有返回值的属性和方法将被配置为返回由 AutoFixture 创建的对象。
您现在最好的选择是开始使用 AutoConfiguredMoqCustomization
并降级到 earlier version of Moq其中只读属性仍在正确配置。
关于c# - AutoFixture AutoMoq 不为某些属性创建模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153930/
我刚刚更改了我的 [RegularExpression] 验证,我三分之一的单元测试失败了! 事实证明 AutoFixture 正在根据该正则表达式生成值,这很酷,但它不理解所有正则表达式,所以我想为
我使用 Autofixture 作为 SUT 工厂,但在卡住空实例时遇到困难。 我想做这样的事情: _fixture.Freeze(c => null); 但很快就意识到这是错误的。我已经用这个解决了
我想以不确定的方式自动生成方法的返回值,即每次调用/测试运行时,我都希望方法返回随机值。目前它总是返回方法调用的默认值: public interface IReturn {
给定这些类: public class DrumAndBassBand { public Drums Drum { get; set; } public Bass Bass { get
我正在将我的测试移植到 AutoFixture 2.0 ,而且我遇到了一些我既无法解释也无法解决的奇怪行为。这个简单的测试对我来说失败了: var autoFixtures = new Fixture
我正在研究一个相当嵌套的模型,它有一些循环引用。它还使用 Entity Framework ,因此所有列表都是 ICollection .为了适应这一点,我正在像这样配置 AutoFixture: _
我正在尝试使用 NSubstitute (1.8.2)、AutoFixture (3.33) 和 AutoFixture.AutoNSubstitute (3.33),如下所示: 我有一个 poco,
当我添加一个新的 TracingBehavior到自动夹具 Fixture实例使用 fixture.Behaviors.Add(new TracingBehavior()); 我在我的 R# 单元测试
我目前正在使用 EF Core Code First Approach,并且我有以下域类: public class Employee { // several properties such a
我刚刚开始使用 AutoFixture,我正在了解基础知识(从我所看到的还有更多),但我遇到了一个问题,我不能 100% 确定对于此类事情的最佳实践是什么。 我正在测试一个 Controller ,该
我在我的单元和集成测试中使用 AutoFixture 并遇到了问题。我正在生成数据传输对象,其中一些类在属性上具有 DataAnnotation 属性(其中一些是自定义的)。 AutoFixture
如何设置确定性测试以验证列表中的项目是否已排序? 首先,我执行以下操作: public void SyncListContainsSortedItems( [Frozen] SyncItem[
默认情况下,AutoFixture在“本地未指定时间”中创建DateTime结构。 我一直在尝试找到一种方法来配置它以创建具有UTC类型的DateTime结构,但是到目前为止没有成功。 有没有办法做到
是否可以在卡住后解冻对象类型? 如果我有一个使用 DateTime 的对象 Appointment,有没有办法做这样的事情? var time = fixture.Freeze(); IEnumera
有什么办法可以给 AutoFixture 一个对象的实例,让它通过所有的 setter 并设置随机数据? wiki 示例仅显示如何从 AutoFixture 获取实例,例如 var autoGener
在我使用 AutoFixture 之前的日子里,我可能做了以下安排来设置名为 CustomerService 的服务的单元测试: public void TestName() { //Arrang
我正在尝试编写这个简单的测试: var fixture = new Fixture().Customize(new AutoMoqCustomization()); var postProcessin
我正在尝试使用 SUT Factory “模式”来创建我的 SUT。 给定 SUT 结构: namespace MySut { public class Dep1 { }
我正在使用的域模型有很多循环引用。事实上,可以从图中的任何点到达大多数对象。许多这些循环引用也在集合中。所以一个 Booking将收藏 Students其中有 Courses 的集合其中有 Booki
我遇到了这个错误。 Ploeh.AutoFixture.Kernel.IllegalRequestException : A request for an IntPtr was detected. T
我是一名优秀的程序员,十分优秀!