单元测试 Monorail 的 RenderText 方法

发布于 2024-08-25 20:46:17 字数 1529 浏览 4 评论 0原文

我正在对用 Monorail v1.0.3 编写的旧 Web 应用程序进行一些维护。我想对使用 RenderText() 的操作进行单元测试。如何提取测试中的内容?从controller.Response.OutputStream读取不起作用,因为响应流要么在PrepareController()中没有正确设置,要么在RenderText()中关闭。

示例操作

public DeleteFoo( int id )
{
    var success= false;
    var foo = Service.Get<Foo>( id );
    if( foo != null && CurrentUser.IsInRole( "CanDeleteFoo" ) )
    {
        Service.Delete<Foo>( id );
        success = true;
    }

    CancelView();
    RenderText( "{ success: " + success + " }" );
}

示例测试(使用最小起订量)

[Test]
public void DeleteFoo()
{
    var controller = new FooController ();
    PrepareController ( controller );

    var foo = new Foo { Id = 123 };

    var mockService = new Mock < Service > ();
    mockService.Setup ( s => s.Get<Foo> ( foo.Id ) ).Returns ( foo );
    controller.Service = mockService.Object;

    controller.DeleteTicket ( foo.Id );

    mockService.Verify ( s => s.Delete<Foo> ( foo.Id ) );
    Assert.AreEqual ( "{success:true}", GetResponse ( Response ) );
}

// response.OutputStream.Seek throws an "System.ObjectDisposedException: Cannot access a closed Stream." exception
private static string GetResponse( IResponse response )
{
    response.OutputStream.Seek ( 0, SeekOrigin.Begin );
    var buffer = new byte[response.OutputStream.Length];
    response.OutputStream.Read ( buffer, 0, buffer.Length );
    return Encoding.ASCII.GetString ( buffer );
}

I'm doing some maintenance on an older web application written in Monorail v1.0.3. I want to unit test an action that uses RenderText(). How do I extract the content in my test? Reading from controller.Response.OutputStream doesn't work, since the response stream is either not setup properly in PrepareController(), or is closed in RenderText().

Example Action

public DeleteFoo( int id )
{
    var success= false;
    var foo = Service.Get<Foo>( id );
    if( foo != null && CurrentUser.IsInRole( "CanDeleteFoo" ) )
    {
        Service.Delete<Foo>( id );
        success = true;
    }

    CancelView();
    RenderText( "{ success: " + success + " }" );
}

Example Test (using Moq)

[Test]
public void DeleteFoo()
{
    var controller = new FooController ();
    PrepareController ( controller );

    var foo = new Foo { Id = 123 };

    var mockService = new Mock < Service > ();
    mockService.Setup ( s => s.Get<Foo> ( foo.Id ) ).Returns ( foo );
    controller.Service = mockService.Object;

    controller.DeleteTicket ( foo.Id );

    mockService.Verify ( s => s.Delete<Foo> ( foo.Id ) );
    Assert.AreEqual ( "{success:true}", GetResponse ( Response ) );
}

// response.OutputStream.Seek throws an "System.ObjectDisposedException: Cannot access a closed Stream." exception
private static string GetResponse( IResponse response )
{
    response.OutputStream.Seek ( 0, SeekOrigin.Begin );
    var buffer = new byte[response.OutputStream.Length];
    response.OutputStream.Read ( buffer, 0, buffer.Length );
    return Encoding.ASCII.GetString ( buffer );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜尕妞 2024-09-01 20:46:18

覆盖 BaseControllerTest.BuildResponse() 并提供使用 Moq 构建的 IMockResponse 模拟。

Override BaseControllerTest.BuildResponse() and provide your mock of IMockResponse built with Moq.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文