2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ASP.NET開発でもユニットテストがしたい

Last updated at Posted at 2016-06-30
1 / 12

概要

.Net Core(最近1.0がリリースされましたね)より昔のASP.NETは、IISとがっつり手を握り合っていて、スタティックなオブジェクトがいっぱいあります。

そんなスタティックなオブジェクトを使っていると、ユニットテストを書けません(書いてもぬるぽです)。

それをどうにかしようというのがこの記事です。


HttpContext


リクエストやレスポンスが入っている肝心かなめのオブジェクトです。

HttpContextBaseという抽象クラスがありますが、なんとHttpContextはこれを継承していません。 :open_mouth:


HttpContextの解決策


HttpContextBaseとHttpContextWrapperを使いましょう。

Injectee.vb
Public Sub New (httpContext As HttpContextBase)
Injector.vb
New Injectee(New HttpContextWrapper(HttpContext.Current))

FormsAuthentication


Form認証にしたときに使うやつです。


FormsAuthenticationの解決策


用意されているラッパはないので、自前でラッパを用意しましょう。

IAuthenticationWrapper.vb
Public Interface IAuthenticationWrapper
    Sub SetAuthentication(userName As String, isPersistent As Boolean)
End Interface
AuthenticationWrapper.vb
Public Class AuthenticationWrapper
    IAuthenticationWrapper

    Sub SetAuthentication(userName As String, isPersistent As Boolean) Implements IAuthenticationWrapper.SetAuthentication
        FormsAuthentication.SetCookeiAuthentication(userName, isPersistent)
    End Sub
End Class

まとめ


ASP.NETでユニットテストしたいときは、

とにかくラッパ!!

:stuck_out_tongue_closed_eyes:

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?