编写单元测试,无需模拟框架

问题描述 投票:-1回答:2

我正在使用NUnit和NSubstiture进行一些单元测试。

我有这门课:

public class Presenter
{
    public Presenter()
    {
    }

    private readonly IView _view;
    public Presenter(IView view)
    {
        _view = view;
        this._view.Loaded += OnLoaded;
    }
    private void OnLoaded()
    {
        _view.Render("Hello Word");
    }
}

我有这个界面:

public interface IView
{
    event Action Loaded;
    void Render(string text);
}

我已经使用NSubstiture框架进行了单元测试,如下所示:

[Test]
public void ctor_WhenViewIsLoaded_CallsViewRender_WithMockingFramework()
{
    var mockView = Substitute.For<IView>();
    Presenter p = new Presenter(mockView);
    mockView.Loaded += Raise.Event<Action>();
    mockView.Received().Render(Arg.Is<string>(s => s.Contains("Hello World")));
}

但是现在我只想测试目的,编写相同的单元测试,但是没有NSubstiture框架:

[Test]
public void ctor_WhenViewIsLoaded_CallsViewRender_WithoutMockingFramework()
{
    IView view;
    Presenter MockingVIew = new Presenter(view);
}

但是怎么做呢?

谢谢

我试试这样:

public class FakePresenter : IView
{
    public event Action Loaded;

    public void Render(string text)
    {
    }
}

[Test]
public void ctor_WhenViewIsLoaded_CallsViewRender_WithoutMockingFramework()
{
    //FakeMockingVIew = new Presenter(view);
    FakePresenter fPresenter = new FakePresenter();
    Presenter p = new Presenter(fPresenter);
    fPresenter.Loaded += Raise.Event<Action>();
    fPresenter.Received();
    Assert.That(fPresenter, Is.EqualTo());
}
c# nunit nsubstitute
2个回答
1
投票

如果您不再想使用模拟框架,那么没有什么能阻止您自己创建一个从IView派生的类并将其用作测试中的依赖项

public class MyTestClass {

    public class FakePresenter : IView {
        public event Action Loaded = delegate { };

        public void Render(string text) {
            RenderedText = text;
        }

        public string RenderedText { get; private set; }

        public void Load() {
            Loaded();
        }
    }

    [Test]
    public void ctor_WhenViewIsLoaded_CallsViewRender_WithoutMockingFramework() {
        //Arrange
        var fake = new FakePresenter();
        var subject = new Presenter(fake);
        var expected = "Hello Word";

        //Act
        fake.Load();
        var actual = fake.RenderedText;

        //Assert
        Assert.AreEqual(expected, actual);
    }
}

上面的依赖实现公开了一个Load()方法来为所有订阅者引发事件,并且还有一个RenderedText属性来捕获传递给Render方法的文本,以便可以根据该值进行断言。


0
投票

当你使用NSubstitute时,你必须告诉模拟视图引发一个事件。但由于IView接口不允许你触发事件,只添加一个事件监听器,NSubstitute做一个workaroud,通过附加一个特殊的事件处理程序,它实际上触发了一个事件(我不熟悉NSubstitute,但我假设这是发生的事情):

// Code here says "attact an event handler", but NSubstitute recognizes this 
// special event handler and raises the event to the "real" hanlders instead
mockView.Loaded += Raise.Event<Action>();

因此,当你离开NSubstitute时,你需要从假视图类中以“正确”的方式触发事件:

public class FakeView : IView
{
    private string RenderedText { get; private set; }
    public event Action Loaded;

    public void Render(string text)
    {
        renderedText = text;
    }

    public void RaiseLoaded() {
        if (Loaded != null) Loaded();
    }
}

现在,您可以轻松触发测试中的事件:

[Test]
public void ctor_WhenViewIsLoaded_CallsViewRender_WithoutMockingFramework()
{
    FakeView view = new FakeView();
    Presenter p = new Presenter(fPresenter);
    view.RaiseLoaded();
    Assert.That(view.RenderedText, Is.EqualTo("Hello World"));
}
© www.soinside.com 2019 - 2024. All rights reserved.