Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Last active December 23, 2015 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyrmn/6561730 to your computer and use it in GitHub Desktop.
Save hyrmn/6561730 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
Cool Cat List:<br />
@Each.Cats
@Current.Name is @Current.Coolness cool <br />
@EndEach
</body>
</html>
public class CatsModule : NancyModule
{
public CatsModule()
{
var db = Database.Open();
Get["/"] = _ => View["CatFancy", new { Cats = db.Cats.All() }];
}
}
public class CatTests
{
private readonly Browser browser;
private dynamic db;
public CatTests()
{
Database.UseMockAdapter(new InMemoryAdapter());
db = Database.Open();
db.Cats.Insert(Id: 1, Name: "Alice", Coolness: "Sorta");
db.Cats.Insert(Id: 2, Name: "Marley", Coolness: "Rather");
browser = new Browser(
with =>
{
with.Module<CatsModule>();
with.ViewFactory<TestingViewFactory>();
});
}
public void it_gets_cats_because_cats()
{
var response = browser.Get("/", with => with.HttpRequest());
(response.Model().Cats as object).ShouldBe(db.Cats.All() as object);
}
}
internal class Program
{
private static void Main(string[] args)
{
var uri = new Uri("http://localhost:8579");
var config = new HostConfiguration { RewriteLocalhost = false };
using (var host = new NancyHost(config, uri))
{
host.Start();
Console.WriteLine("Your application is running on " + uri);
Console.WriteLine("Press any [Enter] to close the host.");
Console.ReadLine();
}
}
}
//For testing, we'll copy our view models into a well known location so that we can easily pull them back to assert
public class TestingViewFactory : IViewFactory
{
private DefaultViewFactory _defaultViewFactory;
public TestingViewFactory(DefaultViewFactory defaultViewFactory)
{
_defaultViewFactory = defaultViewFactory;
}
public Response RenderView(string viewName, dynamic model, ViewLocationContext viewLocationContext)
{
viewLocationContext.Context.Items["###ViewModel###"] = model;
return _defaultViewFactory.RenderView(viewName, model, viewLocationContext);
}
}
public static class BrowserResponseExtensions
{
public static dynamic Model(this BrowserResponse response)
{
return response.Context.Items["###ViewModel###"];
}
}
@darrencauthon
Copy link

I miss simple.data.

When I last looked at Nancy, it was still relatively new and a little rough. I said at the time, "Calling it anything close to Sinatra is not true." It's definitely grown up since then.

@hyrmn
Copy link
Author

hyrmn commented Sep 14, 2013

It gets even slicker with the content negotiation stuff.

@hyrmn
Copy link
Author

hyrmn commented Sep 14, 2013

I'm also really liking Fixie. It's a test runner with no assert library built in and requires no attributes. Default convention is anything ending in 'Tests' is a test class, but you can override that... plus you can override conventions for just about any other test setup / teardown / env plumbing you could want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment