jeudi 28 janvier 2016

why unit test mocks always null with controller

Want to unit test uses mocks my data is actually stored in memory,Why always get null from controller response result.

Here is my controller

private readonly IUnitOfWorkAsync _unitOfWorkAsync;
        private readonly IVersionService _versionService;
        public VersionController(IUnitOfWorkAsync unitOfWorkAsync, IVersionService versionService)
        {
            this._unitOfWorkAsync = unitOfWorkAsync;
            this._versionService = versionService;
        }


        public ActionResult Index()
        {
            var versions =  _versionService.Queryable();
            return View(versions);
        }

Here is my unit test code:

private Mock<IVersionService> _versionServiceMock;
        private Mock<IUnitOfWorkAsync> _unitOfWorkAsync;

        VersionController objController;
        List<Model.Models.Version> listVersion;

        [TestInitialize]
        public void Initialize()
        {

            _versionServiceMock = new Mock<IVersionService>();
            _unitOfWorkAsync = new Mock<IUnitOfWorkAsync>();
            objController = new VersionController(_unitOfWorkAsync.Object, _versionServiceMock.Object);

            listVersion = new List<Model.Models.Version>() {
             new Model.Models.Version() { AppName="App 1",ObjectState=ObjectState.Added,AuditField=new AuditFields()},
             new Model.Models.Version() { AppName="App 2",AppVersion="1.0",ObjectState=ObjectState.Added,AuditField=new AuditFields()},
             new Model.Models.Version() { AppName="App 3",ObjectState=ObjectState.Added,AuditField=new AuditFields()}
            };
        }

        [TestMethod]
        public void Version_Get_All()
        {
            //Arrange
            _versionServiceMock.Setup(x => x.Query().Select()).Returns(listVersion);

            //Act
            var result = (( objController.Index() as ViewResult).Model) as List<Model.Models.Version>;


        }

Why in result always get null,How to check why null is getting.

Note: i use this pattern in my project controller.

Aucun commentaire:

Enregistrer un commentaire