Passing the mocked parameter to mock the interface

I am using nuit with moq to test my controllers.

I am using a session class that has an interface, and the HttpContext is injected into the constructor using ninject. like this

      public class SessionService : ISession
        {
            public HttpContext Context { get; set; }

            public SessionService(HttpContext context)
            {
                this.Context = context;
            }
    }


       public interface ISession
        {
            HttpContext Context { get; set; }
    }



   public HomeController(ISession session)
        {
            _session = session;

        }

I think in order to check the controller, I first mock the HttpContext, and then pass this object to the constructor of the wearing ISession. I still have it

 [Test]
 public void index_returns_view()
        {
             //arrange
            var mockHttpContext = new Mock<HttpContext>();
            var mockContext = new Mock<ISession>(mockHttpContext);
            var c = new HomeController(mockContext.Object);
            //act
            var v = c.Index() as ViewResult;
            //assert
            Assert.AreEqual(v.ViewName, "Index", "Index View name incorrect");
         }

which builds but nunit returns the following error when running the test

System.NotSupportedException: The type for bullying must be an interface or an abstract or unsealed class.

Thanks for the help.

+5
source share
1 answer

HttpContextBase . HttpContext .

  public class SessionService : ISession 
    { 
        public HttpContextBase Context { get; set; } 

        public SessionService(HttpContextBase context) 
        { 
            this.Context = context; 
        } 
} 

unit test, "mockHttpContext.Object" HttpContextBase.

 [Test]    
 public void index_returns_view()    
        {    
             //arrange    
            var mockHttpContext = new Mock<HttpContextBase>();    
            var mockContext = new Mock<ISession>(mockHttpContext.Object);    
            var c = new HomeController(mockContext.Object);    
            //act    
            var v = c.Index() as ViewResult;    
            //assert    
            Assert.AreEqual(v.ViewName, "Index", "Index View name incorrect");    
         } 
+2

All Articles