Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Created March 13, 2015 10:26
Show Gist options
  • Save PureKrome/7eed45a0f248b769675f to your computer and use it in GitHub Desktop.
Save PureKrome/7eed45a0f248b769675f to your computer and use it in GitHub Desktop.
Inconsistent Nancy testing when deserializing some json response.
using System.Collections.Generic;
using System.Linq;
using Nancy;
using Nancy.Testing;
using Newtonsoft.Json;
using Shouldly;
using Xunit;
namespace ConsoleApplication4
{
// install-package xunit
// install-package Nancy.Testing
// install-package newtonsoft.json
public class SampleModule : NancyModule
{
public SampleModule() : base("/")
{
Get["/"] = _ =>
{
var model = new FakeData
{
Message = "Something bad happened.",
Errors = new Dictionary<string, string>
{
{"notificationId", "blah blah blah"}
}
};
return Response.AsJson(model);
};
}
}
public class SampleRepo
{
[Fact]
public void DoTest()
{
// Arrange.
var browser = new Browser(with => { with.Module<SampleModule>(); });
// Act.
var result = browser.Get("/");
// Assert.
var modelWithBadKey = result.Body.DeserializeJson<FakeData>();
var modelWoot = JsonConvert.DeserializeObject<FakeData>(result.Body.AsString());
const string notificationKey = "notificationId";
modelWoot.Errors.First().Key.ShouldBe(notificationKey);
modelWithBadKey.Errors.First().Key.ShouldBe(notificationKey);
}
}
public class FakeData
{
public string Message { get; set; }
public IDictionary<string, string> Errors { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment