示例#1
0
        public void CreateListing_saves_a_listing_via_context()
        {
            var mockListing = new Mock<Listing>();
            var mockListingLocation = new ListingLocation();
            mockListingLocation.ListingLocationLat = "59.11";
            mockListingLocation.ListingLocationLng = "17.22";
            ICollection<ListingLocation> mockListingLocations = new List<ListingLocation>();
            mockListingLocations.Add(mockListingLocation);
            //Mock the properties
            mockListing.SetupProperty(listing => listing.Title, "Test Insert Listing");
            //.SetupProperty(listing => listing.ListingLocations, mockListingLocations);

            var mockSet = new Mock<DbSet<Listing>>();
            var mockSet1 = new Mock<DbSet<ListingLocation>>();

            var mockContext = new Mock<DbConnectionContext>();
            mockContext.Setup(m => m.Listings).Returns(mockSet.Object);

            var service = new EF_ListingRepository(mockContext.Object);
            service.CreateNewListing(mockListing.Object);

            mockSet.Verify(m => m.Add(It.IsAny<Listing>()), Times.Once());
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
        public ActionResult Create(
            [Bind(Include = "Id, DateAvailable ,Address, Description, Title, Telephone, Email")] Listing listing)
        {
            if (ModelState.IsValid)
            {
                listing.DateListed = DateTime.Now;
                listing.Status = SiteConstants.PropertyStatus.ForRent;
                listing.ListingExpiry = DateTime.Now.AddMonths(1);
                listing.ListingUpdated = listing.DateListed;
                listing.ListingAddedBy = Profile.UserName;
                listing.ListingUpdatedBy = listing.ListingAddedBy;
                listing.ListingType = SiteConstants.ListingType.ForRent;
                listing.ListingLocations = new List<ListingLocation>();

                var i = 1;
                var loc = "Pin-" + i + "-lat";
                var lat = Request[loc];
                var loclng = "Pin-" + i + "-lng";
                var lng = Request[loclng];
                if (lat != null && lng != null)
                {
                    var x = new ListingLocation();
                    x.ListingId = 0;
                    x.ListingLocationLat = lat;
                    x.ListingLocationLng = lng;
                    listing.ListingLocations.Add(x);
                }
                while (lat != null && lng != null)
                {
                    i++;
                    loc = "Pin-" + i + "-lat";
                    loclng = "Pin-" + i + "-lng";

                    lat = Request[loc];
                    lng = Request[loclng];
                    if (lat != null && lng != null)
                    {
                        var x = new ListingLocation();
                        x.ListingLocationLat = lat;
                        x.ListingLocationLng = lng;
                        x.ListingLocationPoint = GeoLocation.CreatePoint(Convert.ToDouble(lat), Convert.ToDouble(lng));
                        listing.ListingLocations.Add(x);
                    }
                }

                db.Listings.Add(listing);
                db.SaveChanges();

                string mystring = ViewBag.Files;
                if (mystring != null)
                {
                    var query = from val in mystring.Split(',')
                        select int.Parse(val);
                    foreach (var num in query)
                    {
                        var original = db.ListingsImageGalleries.Find(num);

                        if (original != null)
                        {
                            original.ListingID = listing.Id;
                            db.SaveChanges();
                        }
                    }
                }

                return RedirectToAction("Index");
            }

            return View(listing);
        }