I will explain one of the patterns that I usually use when writing unit tests. Apart from the MOQ library, I use Fluent Assertions(http://

Now, when I will complete this test, it will be as in the following:
- [TestMethod]
- public void CheckStringValue()
- {
- string actual = "Rahul Sahay";
- actual.Should().StartWith("Ra").And.EndWith("ay");
- }

Similarly, to verify that a collection contains a specified number of elements and that all elements match a predicate, you can test as shown in the following code snippet.
- [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");
- }

Let me show it against one of the infrastructure code where I use it.
- [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");
- }
- private static Mock<IMovieRepository> MovieRepository(out IEnumerable<Movie> movie)
- {
- Mock<IMovieRepository> mockMovieRepository = new Mock<IMovieRepository>();
- //Mock return as GetMovies returns that, 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;
- }

Thanks for joining me. I hope you will like it.

Guest UserPosted Jun 9, 2015, 10:22 PM
thanks for sharing, i'll use this nuget package
Pankaj Kumar ChoudharyPosted Jun 9, 2015, 10:19 AM
Nice Article Sir............
Saroj Kumar SahuPosted Jun 9, 2015, 1:08 AM
Thanks for sharing...
Santhakumar MunuswamyPosted Jun 8, 2015, 3:05 PM
Thanks for good show
Ramesh MaruthiPosted Jun 8, 2015, 11:09 AM
Good one, similar to mocking test....
Neeraj KumarPosted Jun 8, 2015, 2:04 AM
good