Skip to content

Instantly share code, notes, and snippets.

@bleroy
Last active June 26, 2016 19:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bleroy/2a7072df8f979dba2bc0ae8778455ac8 to your computer and use it in GitHub Desktop.
Save bleroy/2a7072df8f979dba2bc0ae8778455ac8 to your computer and use it in GitHub Desktop.
Tips for the Week in .NET
@ivaylokenov
Copy link

@bleroy I can suggest a project I have been working on for quite some time now - MyTested.AspNetCore.Mvc - a fluent testing framework for ASP.NET Core MVC. Currently in preview state - written for RC2 and fully supports controller and route tests for now. It is free for individuals, open-source projects, startups and educational institutions. Otherwise it allows up to 500 assertions per test project for free.

Here is a sample controller test written with the library:

// instantiates controller with the registered services
// and sets options for the current test
// and sets session for the current test
// and sets DbContext for the current test
// and tests for added cache entry by the action
// and tests model from view result
MyMvc
    .Controller<MvcController>()
    .WithOptions(options => options
        .For<AppSettings>(settings => settings.Cache = true))
    .WithSession(session => session
        .WithEntry("Session", "SessionValue"))
    .WithDbContext(db => db.WithEntities(entities => entities
        .AddRange(SampleDataProvider.GetModels())))
    .Calling(c => c.SomeAction())
    .ShouldHave()
    .MemoryCache(cache => cache
        .ContainingEntry(entry => entry
            .WithKey("CacheEntry")
            .WithSlidingExpiration(TimeSpan.FromMinutes(10))))
    .AndAlso()
    .ShouldReturn()
    .View()
    .WithModelOfType<ResponseModel>()
    .Passing(m =>
    {
        Assert.AreEqual(1, m.Id);
        Assert.AreEqual("Some property value", m.SomeProperty);
    });

And here is a sample route test:

// tests a route for correct controller, action and resolved route values
MyMvc
    .Routes()
    .ShouldMap(request => request
        .WithLocation("/My/Action/1")
        .WithMethod(HttpMethod.Post)
        .WithAuthenticatedUser()
        .WithAntiForgeryToken()
        .WithJsonBody(new
        {
            Integer = 1,
            String = "Text"
        }))
    .To<MyController>(c => c.Action(1, new MyModel
    {
        Integer = 1,
        String = "Text"
    }))
    .AndAlso()
    .ToValidModelState();

And finally - this is the official web site: https://mytestedasp.net/

@RehanSaeed
Copy link

@andrewlock
Copy link

I wrote a post about reloading application settings when the file changes - it only applies to RC2 (removed for RTM) but will be back again soon!

Reloading strongly typed Options on file changes in ASP.NET Core RC2

Thanks Bertrand, love this newsletter!

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