Last active
December 23, 2015 01:39
-
-
Save hyrmn/6561730 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CatsModule : NancyModule | |
{ | |
public CatsModule() | |
{ | |
var db = Database.Open(); | |
Get["/"] = _ => View["CatFancy", new { Cats = db.Cats.All() }]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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###"]; | |
} | |
} |
It gets even slicker with the content negotiation stuff.
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
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.