Error in Unit Test MVC Controller using Fake

Debug_mode

I am trying to do Unit Testing of MoviesContrller. Controller is created as below,

  public class MoviesController : Controller
{
    private readonly IMovieRepository db;

    public MoviesController(IMovieRepository db)
    {
        this.db = db;

    }

  public ActionResult Index(string movieGenre, string searchString, string BinderList)
    {



        var movies = db.GetAllMovies();

        return View(movies);
    }
   }
   }

To do unit testing of Index action, I have created FakeRepository as below,

  public class FakeMovieRepository : IMovieRepository
     {

        public bool WasGetAllMoviesCalled { private set; get; }

        public IQueryable<Movie> GetAllMovies()
           {
            WasGetAllMoviesCalled = true;
            List<Movie> lstMovies = new List<Movie>
                {
                    new Movie
                    {
                        Rating = "PG",
                        ID = 1,
                        Title = "When Harry Met Sally",
                        Price = 7.99M,
                        Genre = "Romantic Comedy",
                        ReleaseDate = Convert.ToDateTime("1/11/1989 12:00:00 AM")
                    }
                };

             return lstMovies as IQueryable<Movie>;

  }
  }

I have written Unit Test on Index action as below,

  [TestMethod]
    public void ReturnAllMoviesGivenEmptyStringParameters()
    {

       //Arange 
        FakeMovieRepository repo = new FakeMovieRepository();
        var  moviesController = new MoviesController(repo);


        //Actual
        var result = moviesController.Index(String.Empty, String.Empty,String.Empty ) as     ViewResult;

        // returning null always :(  on debigging found that calling fake repo function but returning null !
        var movies = result.Model as IQueryable<Movie>;

        // Assert
        Assert.AreEqual(true, repo.WasGetAllMoviesCalled);

    }

As you see in the comment that always result.model returns null whereas WasGetAllMoviesCalled get returns as true. I put a breakpoint to debug test.

BreakPoint on Index

Db.GetAllMovies() returns List of Movie. But to my surprise values of movies is always null. I am sure I am doing something stupid here. Keep in mind that db.GetAllMovies() is calling GetAllMovies function of FakeMovieRepository. Ideally movies should contain List of movies returned from db.GetAllMovies()

Any help on same? How to unit test controller without using mock.

pero

List< T > doesn't implement IQueryable< T >.

Look http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Moq to unit test Controller Routing function in MVC6

From Dev

How to Unit Test an MVC Controller with Three Dependencies, Using Moq

From Dev

Create Unit Test for MVC 5 Controller with Owin

From Dev

Error in test method in mvc for routing unit test

From Dev

Controller Unit Test fails with SQLSTATE[42000] error

From Dev

How to unit test a Controller action using the Response property in ASP.NET 5 (MVC 6)?

From Dev

Unit Test Web Api Controller fake ApiController.User with custom Identity doesn't work

From Dev

Spring controller unit testing with spring-test-mvc is failing

From Dev

MVC RouteData null in controller when invoked via Unit Test

From Dev

C# ASP.NET MVC Controller unit test

From Dev

Spring controller unit testing with spring-test-mvc is failing

From Dev

C# ASP.NET MVC Controller unit test

From Dev

Whats tests would sufficiently Unit Test a "Create" controller method in MVC?

From Dev

How do I use a Spring MVC Controller as a Fake Endpoint for an Integration Test?

From Dev

How to fake SFTP server on Mule unit test?

From Dev

Unit test of controller angularjs

From Dev

Unit test angularjs controller

From Dev

angular unit test - controller

From Dev

Unit testing a MVC controller

From Java

Using Spring MVC Test to unit test multipart POST request

From Dev

Linking error in unit test using URLRequestConvertible

From Dev

Grails - Unit test controller that's using a named marshaller

From Dev

unit test method that uses identity in regular mvc controller (not web api controller)

From Dev

unit test method that uses identity in regular mvc controller (not web api controller)

From Dev

Using Spring security test to test a secured Spring MVC controller

From Dev

ASP.NET MVC Unit Test Project Entity Framework error

From Dev

ASP.NET MVC Unit Test Project Entity Framework error

From Dev

MVC controller using EntityFramework 404 Error

From Dev

AngularJS controller unit test with Jasmine

Related Related

  1. 1

    Using Moq to unit test Controller Routing function in MVC6

  2. 2

    How to Unit Test an MVC Controller with Three Dependencies, Using Moq

  3. 3

    Create Unit Test for MVC 5 Controller with Owin

  4. 4

    Error in test method in mvc for routing unit test

  5. 5

    Controller Unit Test fails with SQLSTATE[42000] error

  6. 6

    How to unit test a Controller action using the Response property in ASP.NET 5 (MVC 6)?

  7. 7

    Unit Test Web Api Controller fake ApiController.User with custom Identity doesn't work

  8. 8

    Spring controller unit testing with spring-test-mvc is failing

  9. 9

    MVC RouteData null in controller when invoked via Unit Test

  10. 10

    C# ASP.NET MVC Controller unit test

  11. 11

    Spring controller unit testing with spring-test-mvc is failing

  12. 12

    C# ASP.NET MVC Controller unit test

  13. 13

    Whats tests would sufficiently Unit Test a "Create" controller method in MVC?

  14. 14

    How do I use a Spring MVC Controller as a Fake Endpoint for an Integration Test?

  15. 15

    How to fake SFTP server on Mule unit test?

  16. 16

    Unit test of controller angularjs

  17. 17

    Unit test angularjs controller

  18. 18

    angular unit test - controller

  19. 19

    Unit testing a MVC controller

  20. 20

    Using Spring MVC Test to unit test multipart POST request

  21. 21

    Linking error in unit test using URLRequestConvertible

  22. 22

    Grails - Unit test controller that's using a named marshaller

  23. 23

    unit test method that uses identity in regular mvc controller (not web api controller)

  24. 24

    unit test method that uses identity in regular mvc controller (not web api controller)

  25. 25

    Using Spring security test to test a secured Spring MVC controller

  26. 26

    ASP.NET MVC Unit Test Project Entity Framework error

  27. 27

    ASP.NET MVC Unit Test Project Entity Framework error

  28. 28

    MVC controller using EntityFramework 404 Error

  29. 29

    AngularJS controller unit test with Jasmine

HotTag

Archive