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.
1: <connectionStrings>
2: <add name="RavenDB" connectionString="Memory=true" />
3: </connectionStrings>
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;
5:
6: namespace RawStack.Tests.Api
7: {
8: [TestClass]
9: public class MoviesControllerTests
10: {
11: private MoviesController _controller;
12:
13: [TestInitialize]
14: public void TestInitialize()
15: {
16: // Arrange
17: RavenConfig.Register();
18: _controller = new MoviesController();
19: }
20:
21: [TestMethod]
22: public void GetMoviesShouldZeroLoadMovies()
23: {
24: // Act
25: var movies = _controller.GetMovies();
26:
27: // Assert
28: Assert.AreEqual(0, movies.Count());
29: }
30:
31: [TestMethod]
32: public void GetMoviesShouldOneLoadMovies()
33: {
34: // Arrange
35: using (var session = RavenConfig.Store.OpenSession())
36: {
37: session.Store(new Movie());
38: session.SaveChanges();
39: }
40:
41: // Act
42: var movies = _controller.GetMovies();
43:
44: // Assert
45: Assert.AreEqual(1, movies.Count());
46: }
47:
48: [TestMethod]
49: public void GetMoviesShouldTwoLoadMovies()
50: {
51: // Arrange
52: using (var session = RavenConfig.Store.OpenSession())
53: {
54: session.Store(new Movie());
55: session.Store(new Movie());
56: session.SaveChanges();
57: }
58:
59: // Act
60: var movies = _controller.GetMovies();
61:
62: // Assert
63: Assert.AreEqual(2, movies.Count());
64: }
65: }
66: }
And the result is as follows:
Try it
The running application can be found here and the source on GitHub here.
Conclusion
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.
Enjoy!