How do I get my NancyFX unit tests to run my validation? - unit-testing

I'm using NancyFX with FluentValidation, as documented at https://github.com/NancyFx/Nancy/wiki/Nancy-and-Validation. My web app is running fine and validation is working perfectly, but when I try to unit test any of the modules that use validation, I'm getting an error
Nancy.Validation.ModelValidationException : No model validator factory could be located.
Please ensure that you have an appropriate validation package installed, such as
one of the Nancy.Validation packages.
I've verified that my unit test project has references to the Nancy.Validation.FluentValidation and FluentValidation assemblies.
My test code looks like this:
public class ArticleModuleTests {
private Browser browser;
private IDatabase db;
const int USER_ID = 123;
const int ARTICLE_ID = 456;
[SetUp]
public void SetUp() {
var user = new User { Username = "test", Id = USER_ID };
db = A.Fake<IDatabase>();
browser = new Browser(with => {
with.Module<ArticleModule>();
with.RequestStartup((container, pipelines, context) => context.CurrentUser = user);
with.Dependency(db);
});
}
[Test]
public void User_Can_Publish_Article() {
var article = new { title = "Test" };
var result = browser.Post($"/users/{USER_ID}/articles", with => {
with.HttpRequest();
with.Body(JsonConvert.SerializeObject(article));
});
result.StatusCode.ShouldBe(HttpStatusCode.BadRequest);
}
}
My module code is:
public class ArticlesModule : NancyModule {
private IDatabase database;
public ArticlesModule(IDatabase db) {
this.database = db;
Post["/users/{id:int}/articles"] = args => PostArticle(args.id);
}
private dynamic PostArticle(int userId) {
var article = this.Bind<Article>();
var validation = this.Validate(article);
if (!validation.IsValid) return Negotiate.WithModel(validation).WithStatusCode(HttpStatusCode.BadRequest);
database.CreateArticle(userId, article);
return NegotiatorExtensions.WithModel(Negotiate, result)
.WithStatusCode(HttpStatusCode.Created)
.WithHeader("Location", $"http://whatever/users/{userId}/articles/{article.Id}");
}
}
and my validation class is:
public class ArticleValidator : AbstractValidator<Article> {
public ArticleValidator() {
RuleFor(article => article.Title)
.NotEmpty()
.WithMessage("The \"title\" property is required");
RuleFor(article => article.Title)
.Length(2, 50)
.WithMessage("The \"title\" property must be between 2 and 50 characters");
}
}
The NancyFX docs say "Create a validation class... There is no need to register it anywhere as it is automatically detected." - but I'm guessing whatever automatic detection is wired up isn't firing for a unit test project. I'm building on .NET 4.5.2 and using NCrunch as my test runner; what do I need to do to get my test code to pick up the same validation classes as my application modules?

OK, turns out that because NancyFX detects and instantiates validation classes automatically, there's no explicit references in my code to Nancy.Validation.FluentValidation, and so NCrunch is omitting this assembly when building my test project. Setting "Copy referenced assemblies to workspace" in the NCrunch project settings fixed it.

Related

Testing IDbCommandInterceptor with InMemoryDatabase for EFCore

I have written an IDbCommandInterceptor to intercept calls to EntityFrameworkCore DbContext. Now I would like to write unit tests for the interceptor. I am able to intercept and run custom logic for an actual db (tested with SqlServer and MySql), however when testing with an InMemoryDatabase (provided by .Net for testing purposes), my interceptor methods are never called.
Here's how I'm setting up my unit test:
TestDbContext class:
public class TestDbContext : DbContext
{
public TestDbContext(DbContextOptions options) : base(options) { }
public DbSet<User> Users { get; set; }
}
public class User
{
public int UserId { get; set; }
}
This is how I'm initializing the context in my test:
private TestDbContext GetTestDbContext()
{
var options = new DbContextOptionsBuilder<TestDbContext>()
.UseInMemoryDatabase(databaseName: "TestDB")
.AddInterceptors(new MyInterceptor())
.Options;
var context = new TestDbContext(options);
// Seed data
context.Users.Add(new User { UserId = 1 });
context.Users.Add(new User { UserId = 2 });
context.Users.Add(new User { UserId = 3 });
context.SaveChanges();
return context;
}
Now, when I call SaveChanges() or do a query on this context, I expect it to invoke my MyInterceptor's overridden ReaderExecuted() method the same way it does when I use an actual db.
Am I missing something? Or DbCommandInterceptor does not work with InMemoryDatabase?
The InMemory provider is not a relational provider, and therefore does not implement DbCommand interceptors. You can use a SQL Server / SQL Server LocalDb / SQlite database for this kind of testing.

Error when attempting to run my XUnit Tests

I wrote a few tests in XUnit to test my data access layer. I instantiated my DAL objects & configs the same way I would if I were using it in the actual web application(this is configured to run against a dev environment for testing purposes), however XUnit throws an error:
Message: The following constructor parameters did not have matching fixture data: IConfiguration config, IMediaDataAccess media
I'm a bit new to XUnit, so unsure what the problem is. Normally ASP.NET would inject instances of IConfiguration and IMediaDataAccess for me, but that doesn't seem to be the case here.
My Test class & a sample test case:
public class DataAccessLayerTests
{
public IConfiguration Config { get; set; }
private IMediaDataAccess mediaData;
public IMediaDataAccess MediaData { get => mediaData; set => mediaData = value; }
public DataAccessLayerTests(IConfiguration config, IMediaDataAccess media)
{
this.MediaData = media;
this.Config = config;
}
public void GetAllMediaAsync_MediaListIsReturned()
{
List<Media> testData = mediaData.GetAllMedia().Result;
Assert.IsType<List<Media>>(testData);
}
}
The test(s) all fail due to the following error: Message: The following constructor parameters did not have matching fixture data: IConfiguration config, IMediaDataAccess media
For anyone else having this problem, Alexey's comment is correct. You need to download a mocking framework (like Moq) and use it to mock up the dependencies your code is expecting. For example, below is one of my fixed unit tests:
public void IndexDataModel_ShouldDisplayMedia()
{
var mockLogger = new Mock<ILogger<IndexModel>>();
var mockDataAccess = new Mock<IMediaDataAccess>();
mockDataAccess.Setup(media => media.GetAllMedia()).ReturnsAsync(GetTestMedia());
IndexModel indexController = new IndexModel(mockDataAccess.Object, mockLogger.Object);
var result = indexController.OnGet();
var viewResult = Assert.IsType<PageResult>(result);
var model = Assert.IsAssignableFrom<IEnumerable<Media>>(
indexController.mediaList);
}
The trick is you need to mock up anything that you normally depend on getting injected into your constructor, in my case this was:
var mockLogger = new Mock<ILogger<IndexModel>>();
var mockDataAccess = new Mock<IMediaDataAccess>();
My constructor takes both an ILogger and an IMediaDataAccess, so I needed to mock those. Additionally, the other code is there to provide dummy return values when your mocked dependencies are used by the test. This is done with the .Setup line of code. All this does(I think) is when GetAllMedia() is called, the mock object returns the contents of GetTestMedia() instead of needing to make the actual calls. Just make sure whatever function you write has the same return type as the real function. For reference, this is my GetTestMedia() function:
private List<Media> GetTestMedia()
{
var listMedia = new List<Media>();
Media testMedia = new Media
{
Description = "TestDesc",
Genre = "TestGenre",
Name = "TestName",
Rating = MediaRating.Excellent,
Type = MediaType.Movie,
Id = 1
};
listMedia.Add(testMedia);
Media testMedia2 = new Media
{
Description = "TestDesc2",
Genre = "TestGenre2",
Name = "TestName2",
Rating = MediaRating.Poor,
Type = MediaType.Music,
Id = 2
};
listMedia.Add(testMedia2);
return listMedia;
}

Unable to mock Configuration.Formatters using Moq

I am developing an ASP.NET web API application. I am unit testing to every component in my application. I am using Moq Unit Test framework to mock the data. Now I am trying to mock the Configuration.Formatters.JsonFormatter in my Unit test because my action under unit test is using it as follows:
public HttpResponseMessage Register(model)
{
return new HttpResponseMessage
{
StatusCode = HttpStatusCode.BadRequest,
Content = new ObjectContent<List<string>>(errors, Configuration.Formatters.JsonFormatter)
};
}
I am trying to mock the Configuration.Formatters.JsonFormatter in the Unit Test as follow.
[TestMethod]
public void Register_ReturnErrorsWithBadRequest_IfValidationFails()
{
PostUserRegistration model = new PostUserRegistration {
Name = "Wai Yan Hein",
Email = "waiyanhein#gmail.com",
Password = ""
};
Mock<JsonMediaTypeFormatter> formatterMock = new Mock<JsonMediaTypeFormatter>();
Mock<MediaTypeFormatterCollection> formatterCollection = new Mock<MediaTypeFormatterCollection>();
formatterCollection.Setup(x => x.JsonFormatter).Returns(formatterMock.Object);
Mock<HttpConfiguration> httpConfigMock = new Mock<HttpConfiguration>();
httpConfigMock.Setup(x => x.Formatters).Returns(formatterCollection.Object);
Mock<IAccountRepo> accRepoMock = new Mock<IAccountRepo>();
AccountsController controller = new AccountsController(accRepoMock.Object);
controller.Configuration = httpConfigMock.Object;
controller.ModelState.AddModelError("", "Faking some model error");
HttpResponseMessage response = controller.Register(model);
Assert.AreEqual(response.StatusCode, System.Net.HttpStatusCode.BadRequest);
}
When I run my unit tests, it is giving me this error.
System.NotSupportedException: Invalid setup on a non-virtual
(overridable in VB) member: x => x.JsonFormatter
So, how can I fix that error and how can I mock Configuration.Formatters.JsonFormatter?
You should not have to test that the framework is doing what it was designed to do. Instead, similar to what was suggested before in comments, I suggest using the built in methods of the ApiController and also refactoring the action to the syntax suggested from documentation for that version of Asp.Net Web API
public IHttpActionResult Register(PostUserRegistration model) {
if (!ModelState.IsValid)
return BadRequest(ModelState); //<-- will extract errors from model state
//...code removed for brevity
return Ok();
}
A simple example of a unit test for the above method could look something like this...
[TestMethod]
public void Register_ReturnErrorsWithBadRequest_IfValidationFails() {
//Arrange
PostUserRegistration model = new PostUserRegistration {
Name = "Wai Yan Hein",
Email = "waiyanhein#gmail.com",
Password = ""
};
var accRepoMock = new Mock<IAccountRepo>();
var controller = new AccountsController(accRepoMock.Object);
controller.ModelState.AddModelError("", "Faking some model error");
//Act
var response = controller.Register(model) as InvalidModelStateResult; //<-- Note the cast here
//Assert
Assert.IsNotNull(response);
}
If the injected dependency is not going to be referenced/invoked in the method under test you could also forego the mock and inject null. That however is dependent on code not provided in the original question.

Odd NHibernate Sqllite.InMemory behaviour

I am trying to setup database unit tests using the Sqllite.InMemory functionality.
If I run an unit test, everything is working fine. If I run the same test a second time I get an System.Data.SQLite.SQLiteException: no such table: Person
After waiting for a while and/or (?) restarting Visual studio I can run the unit test once again.
Is there something wrong with the configuration or the session handling?
public abstract class InMemoryDatabaseFixture : IDisposable
{
private const string ConnectionString
= "Data Source=:memory:;Version=3;New=True;Pooling=True;Max Pool Size=1;";
private readonly ISessionFactory _sessionFactory;
private readonly ISession _session;
protected InMemoryDatabaseFixture()
{
var config = SQLiteConfiguration.Standard.InMemory().ShowSql().ConnectionString(ConnectionString);
_sessionFactory = Fluently.Configure()
.Database(config)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionContainer>())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
.BuildSessionFactory();
_session = _sessionFactory.OpenSession();
SessionContainer = MockRepository.GenerateMock<ISessionContainer>();
SessionContainer.Stub(sc => sc.SessionFactory).Return(_sessionFactory);
SessionContainer.Stub(sc => sc.Session).Return(_session);
}
protected ISessionContainer SessionContainer { get; private set; }
public void Dispose()
{
_sessionFactory.Dispose();
_session.Dispose();
}
}
Here a simple usage of the base class:
[TestFixture]
public class FinderFixture : InMemoryDatabaseFixture
{
[Test]
public void Test()
{
var finder = new Finder(SessionContainer);
var result = finder.Find();
Assert.That(result, Is.Not.Null);
}
}
Update: After some trials here is finally my working configuration. Exporting the schema after building the SessionFactory is doing the magic.
Configuration configuration = null;
_sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.ExposeConfiguration(cfg => configuration = cfg)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionContainer>())
.BuildSessionFactory();
_session = _sessionFactory.OpenSession();
var export = new SchemaExport(configuration);
export.Execute(true, true, false, _session.Connection, null);
You have requested that connection pooling in the ADO.NET provider be enabled. This will keep the underlying connection active even after NHibernate has closed it.
In my own unit tests, I have simply (based on your original code):
_sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SessionContainer>())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
.BuildSessionFactory();
Also, since the session is generated from the session factory, it would be prudent to dispose all sessions before disposing the session factory.

How to create a test for Fluent nHibernate conventions?

I am creating a set of conventions for Fluent nHibernate. I create a few conventions, like primary key, foreign key and many-to-many table.
I would like to be able to test out these conventions with an in memory database to see if I coded these conventions correctly.
Currently, I am setting up nHibernate using an SQlite in-memory database like this:
Configuration configuration = null;
var factory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory().ShowSql())
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<DataLayer>();
m.FluentMappings.Conventions.AddFromAssemblyOf<DataLayer>();
})
.ExposeConfiguration((c) => configuration = c)
.BuildSessionFactory();
var session = factory.OpenSession();
var export = new SchemaExport(configuration);
export.Execute(true, true, false, session.Connection, null);
I create a test case, but I don't know test a naming convention. How can I achieve some unit tests on a conventions using a Visual Studio test project?
What I did is not testing against the database, but just test the generated mapping. So for example I have a convention that says that all foreign keys are written like ID and I test it like (I use xunit and not mstest, but hopefully you can get the concept...):
[Fact]
public void AddDefaultConventions_ShouldNameMappingToForeinKeyCorrectly()
{
var configuration = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008)
.Mappings(m =>
{
m.FluentMappings.Conventions.Add(new CustomForeignKeyConvention());
m.FluentMappings.Add<TestClassMap>();
m.FluentMappings.Add<TestClass2Map>();
})
.BuildConfiguration();
var typeMapping = configuration.GetClassMapping(typeof(TestClass2));
var property = typeMapping.GetProperty("Parent");
Assert.Equal("ParentID", property.ColumnIterator.First().Text);
}
private class TestClass
{
public virtual int ID { get; set; }
}
private class TestClass2
{
public virtual int ID { get; set; }
public virtual TestClass Parent { get; set; }
}
private class TestClassMap : ClassMap<TestClass>
{
public TestClassMap()
{
Id(x => x.ID);
}
}
private class TestClass2Map : ClassMap<TestClass2>
{
public TestClass2Map()
{
Id(x => x.ID);
References(x => x.Parent);
}
}
Btw. it wouldn't be too difficult to test against a DB, just try to select something from TestClass2 and make sure no exception is thrown... but I think the way I showed is easier and I trust that when FluentNhibernate can generate the correct NHibernate mapping, NHibernate can generate the correct queries for me.