In the previous post we added unit tests for the AngularJS code running on the client. And while these tests are important there is more to test. After all the client side code depends on a server side WebAPI controller and a RavenDB database. In this post we are going to add unit tests for those two.
To inject or not to inject dependencies
Normally when one class depends on another it is best to use dependency injection to get one to use another. And the dependency is then based around interfaces so a dependency can be faked during testing.
The MoviesController is the WebAPI controller that publishes our movies. This results in it having a dependency on the RavenDB IDocumentStore. Now we could create a fake IDocumentStore implementation but the recommendation from Ayende Rahien with RavenDB is not to do so and user the real thing with an in memory database. And because we are already using the EmbeddableDocumentStore that loads its connection info from the application configuration file all we need to do is specify the appropriate connection string for our unit tests.
2: <add name="RavenDB" connectionString="Memory=true" />
With that in place all we need to do is call RavenConfig.Register() before each test.
Tests calling the MoviesController
The unit tests below load the movies through the WebAPI controller a few times with different result. Note that the TestInitialize() function recreates the in memory database between each test. Normally I would not test this function with these three almost identical results but that is just to prove that the database is really reset between each test.
1: using System.Linq;
2: using Microsoft.VisualStudio.TestTools.UnitTesting;
3: using RawStack.Api;
4: using RawStack.Models;
6: namespace RawStack.Tests.Api
9: public class MoviesControllerTests
11: private MoviesController _controller;
14: public void TestInitialize()
16: // Arrange
18: _controller = new MoviesController();
22: public void GetMoviesShouldZeroLoadMovies()
24: // Act
25: var movies = _controller.GetMovies();
27: // Assert
28: Assert.AreEqual(0, movies.Count());
32: public void GetMoviesShouldOneLoadMovies()
34: // Arrange
35: using (var session = RavenConfig.Store.OpenSession())
37: session.Store(new Movie());
41: // Act
42: var movies = _controller.GetMovies();
44: // Assert
45: Assert.AreEqual(1, movies.Count());
49: public void GetMoviesShouldTwoLoadMovies()
51: // Arrange
52: using (var session = RavenConfig.Store.OpenSession())
54: session.Store(new Movie());
55: session.Store(new Movie());
59: // Act
60: var movies = _controller.GetMovies();
62: // Assert
63: Assert.AreEqual(2, movies.Count());
And the result is as follows:
Unit testing the WebAPI and RavenDB parts on the server is also really easy. No need to start with dependency injection if you don’t want to as the RavenDB in memory capabilities make this really simple.
For more information about unit testing a ASP.NET WebAPI 2 controller see my post here.
Index of posts on the RAW stack
See here for more posts on the RAW stack.