Typemock隔离器:模拟未注入的依赖项? [英] Typemock Isolator: Mock a dependency that's not injected?

查看:85
本文介绍了Typemock隔离器:模拟未注入的依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WidgetDoer类取决于未注入的Foo.我需要伪造_fooDoStuffWith()实现(然后确认Do()返回了结果-这是我的真实代码的简化表示).

My WidgetDoer class depends on Foo, which is not injected. I need to fake _foo's implementation of DoStuffWith() (and then verify that Do() returned the result -- this is a simplified representation of my real code).

public class WidgetDoer {
    readonly Foo _foo;

    public WidgetDoer() {
        _foo = new Foo();
    }

    public Bar Do(Widget widget) {
        var result = _foo.DoStuffWith(widget);
        return result;
    }
}

我试图使用以下隔离器语法来防止创建真正的Foo对象(在WidgetDoer()构造函数内部),但是无论如何都将实例化真正的Foo对象:

I tried to use the following Isolator syntax to prevent a real Foo object from being created (inside the WidgetDoer() constructor), but the real Foo object is instantiated anyway:

var fooFake = Isolate.Fake.Instance<Foo>();
Isolate.WhenCalled(() => new Foo()).WillReturn(fooFake);

我可以使用Typemock模拟未注入的依赖项吗?

Can I use Typemock to mock a dependency which is not injected?

推荐答案

此代码使我可以模拟耦合的依赖项:

This code allowed me to mock the coupled dependency:

Isolate.Swap.NextInstance<Foo>().With(FooFake);

请记住, TypeMock支持mscorlib中很少的类型.

这篇关于Typemock隔离器:模拟未注入的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆