Skip to main content

.NET Automated Testing with Session and Microsoft Fakes

I am working on a project that uses session and application level calls nested deep within the code. It is not practical to refactor those calls to pass in the values needed and make Unit Testing possible through dependency injection, so I needed to find a way to create automated tests that would not crash on those calls. I researched and tried out many different approaches. Ultimately what I came up with was to use Microsoft's Fakes to mimmic the Session and Application calls. I was helped greatly by this post,Testing Untestable Code Thanks to MS Fakes, and commented my code with his link in case others at my company need to view it.

I created a class that my Web Tests can inherit from that will handle the setup of these Fake calls.

Imports System.IO
Imports System.Web
Imports System.Web.Fakes
Imports Microsoft.QualityTools.Testing.Fakes

<TestClass()> Public Class SessionTestBase
 Private _fakeHttpContext As IDisposable

 <TestInitialize()> Public Sub BeforeEachTestBase()
  _fakeHttpContext = ShimsContext.Create

  System.Web.Fakes.ShimHttpContext.CurrentGet = Function()
                                                  Return New HttpContext(New HttpRequest("", "", ""), New HttpResponse(New StringWriter()))
                                                End Function


 End Sub

 Private Sub SetupSessionState()
  Dim sessionState As New System.Web.SessionState.Fakes.ShimHttpSessionState
  sessionState.SessionIDGet = Function()
                                Return "1"
                              End Function

  Dim sessionDictionary As New Dictionary(Of String, Object)
  sessionState.ItemSetStringObject = Sub(key As String, value As Object)
                                       sessionDictionary.Add(key, value)
                                     End Sub
  sessionState.ItemGetString = Function(key As String)
                                 If sessionDictionary.ContainsKey(key) Then
                                   Return sessionDictionary(key)
                                   Return Nothing
                                 End If
                               End Function

  System.Web.Fakes.ShimHttpContext.AllInstances.SessionGet = Function(o As Object)
                                                               Return sessionState
                                                             End Function
 End Sub

 Private Sub SetupApplicationState()
  Dim applicationState As New ShimHttpApplicationState
  Dim applicationDictionary As New Dictionary(Of String, Object)
  applicationState.ItemSetStringObject = Sub(key As String, value As Object)
                                           applicationDictionary.Add(key, value)
                                         End Sub
  applicationState.ItemGetString = Function(key As String)
                                     If applicationDictionary.ContainsKey(key) Then
                                       Return applicationDictionary(key)
                                       Return Nothing
                                     End If
                                   End Function

  System.Web.Fakes.ShimHttpContext.AllInstances.ApplicationGet = Function(o As Object)
                                                                   Return applicationState
                                                                 End Function
 End Sub

 <TestCleanup()> Public Sub AfterEachTestBase()
  If _fakeHttpContext IsNot Nothing Then
  End If
 End Sub
End Class


Popular posts from this blog

Converting a Large AngularJS Application to TypeScript Part 1

I work on a project that uses AngularJS heavily. Recently we wondered if using a preprocesser like CoffeeScript or TypeScript for our JavaScript would be beneficial. If our team is going to switch languages, we would need to be able to convert existing code over without much pain and we would have to find enough value in switching that it would be worth the conversion. I had read an article that stated that because TypeScript is a SuperSet of JavaScript, you could convert a plain JavaScript file to TypeScript by changing the extension to .ts and not much else would need to change. I wanted to test out this claim, so I took a file that I was familiar with, an Angular Controller, and tried to convert it to TypeScript to see how much effort it would take and then try to figure out where we would benefit from using TypeScript. This is what the controller JavaScript file looked like to start out with: ( function () { 'use strict' ; angular .module( 'app'

My idea for Hearthstone to add more deck slots

Recently someone asked the Blizzard developers for more slots for decks in the game Hearthstone. The response was that they are talking about it and looking into it, but no decision has been made yet. One of the concerns over adding deck slots is that it could complicate the UI for Hearthstone and make it more difficult for new players to understand. I have what I think would be a good solution to add more deck slots without increasing the learning curve for the game much if at all. First I would take a look at the current selection screen for starting to play a game. It defaults to showing the decks that are custom built by the player if they have any custom decks, and there is an option to page over to the basic decks. This basic deck screen is perfect for how I would change this process. Instead of having 2 pages of decks, 1 for basic and 1 for custom, you would just see the select a Hero screen. Then once you selected the Hero you wanted, you would see all of the decks that

Gamify TDD

I like it when things that would not normally be associated with games add concepts from games as a way to incentives you to accomplish things. Why simply go for a run if you can have an app that will track you and give you a gold star if you do better than you did the last time? Why go to the coffee shop that only gives you coffee if the other one will give you points that you can redeem for free drinks eventually? I was recently introduced to C odeSchool , an online training system similar to PluralSight, it has video courses and challenges you can take to prove that you retained what the video taught. CodeSchool also adds badges and tracks to your learning, so as you complete a video and its challenges you get a badge. Complete a collection of courses within a specific discipline and you become a master of that discipline. Some of these incentives are not tangible and really don't mean much in the real world, but they tend to work for me. If I start working towards a large g