protected object CoreGetService(Type serviceType)
        {
            if (serviceType == null) { throw new ArgumentNullException("serviceType"); }

            object result = null;
            if (serviceType == typeof(IPackageRepository)) {
                result = this.CreatePackageRepository();
            }
            else if (serviceType == typeof(IJsonSearlizer)) {
                result = new JsonNetSearlizer();
            }
            // MVC controllers
            else if (serviceType == typeof(ConfigController)) {
                result = new ConfigController(this.CreatePackageRepository(), this.CreateJsonSearlizer());
            }
            else if (serviceType == typeof(HomeController)) {
                result = new HomeController(this.CreatePackageRepository());
            }
            // API Controllers
            else if (serviceType == typeof(ConfigController)) {
                result = new ConfigController(this.CreatePackageRepository(), this.CreateJsonSearlizer());
            }
            else if (serviceType == typeof(HomeController)) {
                result = new HomeController(this.CreatePackageRepository());
            }

            return result;
        }
        public void TestGetAllPackages()
        {
            IList<Package> allPackages = RandomDataHelper.Instance.CreateRandomListOf<Package>(
                RandomDataHelper.Instance.CreateRandomePackage,
                10);

            var mock = new Mock<IPackageRepository>();
            mock.Setup(repo => repo.GetPackages())
                .Returns(() => {
                    return allPackages.AsQueryable();
                });

            ConfigController controller = new ConfigController(mock.Object, this.CreateSearlizer());
            var result = controller.GetAllPackages();

            Assert.IsNotNull(result);
            IList<Package> expectedResult = allPackages.ToList();
            IList<Package> actualResult = result.ToList();
            CustomAssert.AreEqual(expectedResult, actualResult);

            // ensure GetPackages is called only once
            mock.Verify(
                res => res.GetPackages(),
                Times.Exactly(1));
        }
        public void Test_PostPackage()
        {
            var mockRepo = new Mock<IPackageRepository>();
            mockRepo.Setup(foo => foo
                .AddPackage(It.IsAny<Package>()))
                .Returns<Package>(p => {
                    p.Id = Guid.NewGuid();
                    return p;
                });

            IJsonSearlizer searlizer = this.CreateSearlizer();
            ConfigController controller = new ConfigController(mockRepo.Object, searlizer);

            Package package = RandomDataHelper.Instance.CreateRandomePackage();
            controller.PostPackage(searlizer.Searlize(package));

            // make sure AddPackage is getting called only once
            mockRepo.Verify(
                pkg => pkg.AddPackage(It.IsAny<Package>()),
                Times.Exactly(1));
        }
 public void Test_NullSearlizer()
 {
     var mockRepo = new Mock<IPackageRepository>();
     ConfigController cc = new ConfigController(mockRepo.Object,null);
 }
 public void Test_NullRepository()
 {
     ConfigController cc = new ConfigController(null, this.CreateSearlizer());
 }