我是两个库的新手,在承诺在大型项目中使用它之前,我需要澄清我的单元测试中的低代码工作量自动插件的选项.
在Google上花了一些时间之后我得出结论,与其他一些IOC/Mocking产品配对不同,LightInject + Nsubstitute没有现成的插件库来简化单元安排阶段无操作默认模拟的声明测试.
我已经阅读了关于如何使用临时增强型模拟对象覆盖LightInject容器的LightInject文档,仅用于单元测试的范围,但是单元测试可能触及的所有无操作默认隔离模拟如何.有没有办法在LightInject容器中自动创建它们?
我正在寻找的内部IOC容器行为是:
public class LightInject.ServiceContainer
{
..
public T GetInstance<T)
{
if (( this.RegisteredInterfaces.Any( i => i.Itype == T ) == false )
&& ( this.TemporaryUnitTestOverrides.Any( i => i.Itype == T ) == false ))
&& ( /* this container is configured with an automocking delegate */ ))
return autoMockCreatorDelegate<T>.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
看起来LightInject的IProxy和Interceptors提供了一些内部模拟对象构建块,但是Nsubstitute库是完整的功能.
澄清我的意思默认不做任何模拟和增强模拟.
// default do nothing mock
var calculator = Substitute.For<ICalculator>();
// Enhanced mock that will return 3 for .Add(1,2)
var calculator …Run Code Online (Sandbox Code Playgroud) 我想和温莎一起做自动锁定,以便我可以做类似的事情
_controller = _autoMockingContainer.Create<MyControllerWithLoadsOfDepdencies>();
Run Code Online (Sandbox Code Playgroud)
在Ayende的 Rhino图书馆里曾经有一个Windsor自动模拟容器.但这似乎不再维持,所以依赖关系有点旧(它使用Castle Windsor 2,但我们需要引用2.5),因此导致dll地狱.
有没有可行的替代方案?我尝试从rhino测试中提取出相关的类,但是我可以处理的更多内容.
我想知道是否有可能在MOQ中自动模拟容器而不添加任何MOQ库.我在找到一个自动插入IList的干净方法时遇到了问题.
提前致谢!
我们正在使用Autofac.Extras.Moq.AutoMock.现在我使用Lazy <>有一个构造函数依赖
public MyService(Lazy<IDependency> myLazyDependency) {...}
Run Code Online (Sandbox Code Playgroud)
测试MyService我们需要嘲笑Lazy<Dependency>.
我正在尝试这个
[ClassInitialize]
public static void Init(TestContext context)
{
autoMock = AutoMock.GetLoose();
}
[TestInitialize]
public void MyTestInitialize()
{
var myDepMock = autoMock.Mock<Lazy<IDependency>>(); // <-- throws exception
}
Run Code Online (Sandbox Code Playgroud)
这是测试运行器返回的异常:
初始化方法Tests.MyServiceTests.MyTestInitialize引发异常.System.InvalidCastException:System.InvalidCastException:无法转换类型为'System.Lazy 1[IDependency]' to type 'Moq.IMocked1 [System.Lazy`1 [IDependency]]'的对象..
那么,我如何使用automock传递一个Lazy <>模拟对象.
我已经阅读了Mark Seeman 关于自动模拟的文章,我现在正在编写一个基于该文章的可重复使用的windsor容器.
我执行Mark的文章(基本上直接复制)
主要工作是在AutoMoqResolver课堂上完成的.只要类依赖于接口,这将提供模拟:
public class AutoMoqResolver : ISubDependencyResolver
{
private readonly IKernel kernel;
public AutoMoqResolver(IKernel kernel)
{
this.kernel = kernel;
}
public bool CanResolve(
CreationContext context,
ISubDependencyResolver contextHandlerResolver,
ComponentModel model,
DependencyModel dependency)
{
return dependency.TargetType.IsInterface;
}
public object Resolve(
CreationContext context,
ISubDependencyResolver contextHandlerResolver,
ComponentModel model,
DependencyModel dependency)
{
var mockType = typeof(Mock<>).MakeGenericType(dependency.TargetType);
return ((Mock)this.kernel.Resolve(mockType)).Object;
}
}
Run Code Online (Sandbox Code Playgroud)
将AutoMoqResolver被添加到使用以下实施的容器IWindsorInstaller接口:
public class AutoMockInstaller<T> : IWindsorInstaller
{
public void Install(
IWindsorContainer container,
IConfigurationStore …Run Code Online (Sandbox Code Playgroud) 任何人都可以提供帮助,我在使用Ninject和NSubstitute之间可用的自动模拟时遇到问题,实际上这个包是一个ninject打包调用Ninject.MockingKernel.NSubstitute,应该允许我使用Ninject创建模拟并返回注入模拟的 实例.
Moq和Rhinomocks似乎有一些例子,但我没有看到NSubstitute.
到目前为止我所拥有的是什么
this.kernel = new NSubstituteMockingKernel();
var summaryService = this.kernel.GetMock<IMyService>(); // GetMock not available
Run Code Online (Sandbox Code Playgroud)
有人用吗?
我有一个使用Qt5的项目,并且有一个CMakeLists.txt用于创建Visual Studio解决方案的文件。
这是我的摘录 CMakeLists.txt
cmake_policy(SET CMP0020 NEW)
set(CMAKE_AUTOMOC ON)
find_package(Qt5 REQUIRED COMPONENTS core widgets)
set(COMMON_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src)
include_directories( ${Boost_INCLUDE_DIRS}
${COMMON_INCLUDE_DIR}
)
file(GLOB_RECURSE COMMON_SOURCE "*.hpp" "*.cpp")
add_library(${PROJECT_NAME} ${COMMON_SOURCE})
qt5_use_modules(${PROJECT_NAME} Widgets)
Run Code Online (Sandbox Code Playgroud)
当我尝试编译代码时,它返回以下错误:
>AUTOMOC : error : C:/Users/.../Projects/MyProject/build/MyProjects_automoc.cpp The file includes the moc file "moc_MyFile.cpp", but could not find header "MyFile{.h,.hh,.h++,.hm,.hpp,.hxx,.in,.txx}" in C:/Users/.../Projects/MyProject/build/
Run Code Online (Sandbox Code Playgroud)
moc文件已自动生成,并且标题不在build文件夹中,而是在src目录中的文件夹中。
如何解决此错误?
假设我的上下文配置类似于:
Establish context = () =>
{
...
IFileProcesser processer = new FileProcesser();
The<IFileProcesser>()
.WhenToldTo(x => x.Read(Param<Stream>.IsAnything))
.Return<Stream>(processer.Read);
...
};
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法告诉Machine.Fakes不要伪造IFileProcesser并使用FileProcesser的实现?
automocking ×8
c# ×5
mocking ×2
moq ×2
nsubstitute ×2
unit-testing ×2
autofac ×1
autofixture ×1
cmake ×1
light-inject ×1
moc ×1
mstest ×1
ninject ×1
qt ×1
qt5 ×1
rhino ×1