Fluent Assertions

Hi Friends,

Today, i will talk about one of the patterns which i usually use while writing unit tests. Apart from MOQ library, i also use Fluent Assertions(http://www.fluentassertions.com/). You can search for the same in nuget package manager and install it in the test project. Out of the box, fluent assertions give tons of extensions method which help to easily write assertions on the actual like shown below. In the below example, i am just running the test against one sample string of my name.

1st

Now, when i complete this test, it will look like

 [TestMethod]
        public void CheckStringValue()
        {
            string actual = "Rahul Sahay";
            actual.Should().StartWith("Ra").And.EndWith("ay");
        }

When, i run the same it will produce the below result.

2nd

Similarly, To verify that a collection contains a specified number of elements and that all elements match a predicate, you can test like shown below.

 [TestMethod]
        public void CheckCollection()
        {
            IEnumerable collection = new[] { 1, 2, 3,4,5 };
            collection.Should().HaveCount(6, "because I thought I put 5 items in the collection");
        }

Now, when i run the above test, it will fail. And this will fail for a valid reason. But, look at the error message returned by the same.

3rd

Let, me show the same against one of the infrastructure code where i am using the same.

[TestMethod]
        public void MovieNameTest()
        {
            IEnumerable<Movie> movie;
            var mockMovieRepository = MovieRepository(out movie);

            mockMovieRepository.Setup(obj => obj.GetMovies()).Returns(movie);

            IMovieService movieService = new MovieManager(mockMovieRepository.Object);

            IEnumerable<MovieData> data = movieService.GetDirectorNames();

            data.Should().HaveCount(4, "because we put these many values only");


        }

Now, it has one dependency on MovieRepository(out movie). Basically, this is my arrange.

  private static Mock<IMovieRepository> MovieRepository(out IEnumerable<Movie> movie)
        {
            Mock<IMovieRepository> mockMovieRepository = new Mock<IMovieRepository>();

            //Mock return as GetMovies returns the same, so we are not going to hit db
            //we are going to return mocked up entity
            movie = new Movie[]
            {
                new Movie()
                {
                    MovieName = "Avatar",
                    DirectorName = "James Cameron",
                    ReleaseYear = "2009"
                },
                new Movie()
                {
                    MovieName = "Titanic",
                    DirectorName = "James Cameron",
                    ReleaseYear = "1997"
                }
            };
            return mockMovieRepository;
        }

When, i run the above test, it will again throw similar error because count is 2 and i am checking for 4.

4th

Thanks for joining me. I hope you like it.

Rahul Sahay,
Happy Coding

2,638 total views, 1 views today