Posts Tagged ‘unit-test’

RSpec/unit testing permanent signed cookies

No Comments »

Ran into a simple problem today while I was speccing a new controller in our project. We generate some signed cookies for devices which check into our system and then we use those cookies to identify these devices when they return. Unfortunately the ActionController.TestRequest @request instance variable you get when using rspec-rails has the cookies collection as a basic hash and no cookies= method defined. This means that attempting to invoke permanent or signed on a hash results in a runtime error and you can't just assign the cookies collection to a valid ActionDispatch::Cookies::CookieJar instance.

You can get around this by going around the missing attribute setter and changing the instance variable directly.

#./specs/controllers/some_controller_spec.rb
   # a new device is created here...
   ⋮
   @request.instance_variable_set(:@cookies, ActionDispatch::Cookies::CookieJar.build(@request))
   # the token is what we use to identify the device on subsequent requests
   # so our specs need to be able to create it when necessary
   @request.cookies.permanent.signed[:token] = [device.device_id, device.secret]
   ⋮
   # remainder of spec omitted

If you're doing stuff with the regular cookies hash you will need to merge! your cookies hash with the @request.cookies hash before building and assigning the full blown ActionDispatch::Cookies::CookieJar instance.

Reference: How to test cookies.permanent.signed in Rails 3


Outsmarting ConfigurationManager for unit testing

No Comments »

Today I implemented the ConfigurationSection stuff in .NET you can use to interject your custom configuration structures into config files like web.config and app.config.  I wanted to test not only my implementation, but the classes that rely on the configuration but it wasn't exactly clear how I could get ConfigurationManager to load an arbitrary configuration.

The solution is quite simple and I figured it out after a bit of trial and error.  What got me was that I had a custom configuration file in a child directory of my unit test project("WebServices") and I had set the configuration file's build action to "Copy Always".  I had thought it would end up in "bin\Debug" but it correctly ended up in "bin\Debug\WebServices" so that's an important thing to keep in mind.

Outsmarting ConfigurationManager also requires that you have an accompanying file (empty text file works) that the configuration applies to.  So here's what the start of my [now working] test looks like:

    private static RequestHandlerSection _section;

    [TestFixtureSetUp]
    public void Setup()
    {
        using (var sw =
            File.CreateText(".\\WebServices\\RequestHandlerTests")
        )
        {
            sw.Close();
        }
        var config = ConfigurationManager.OpenExeConfiguration(
            ".\\WebServices\\RequestHandlerTests"
        );
        _section = (RequestHandlerSection)
            config.Sections[RequestHandlerSection.Name];
    }

    [Test]
    public void WorkDamnYouTest()
    {
        Assert.IsNotNull(_section);
        Assert.IsNotEmpty(_section.Security);
    }
}

Ideally at some point in the future ConfigurationManager could get a Load(string xml) or some such so we don't have to deal with this nonsense.  While I could add one with an extension method I'd like to see it get into the framework to improve unit testability.