Set Up Ninject and Moq In A MVC 5 Prjoect
2016-10-05 10:28
204 查看
Install Ninject
![](http://img.blog.csdn.net/20161005094804360?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20161005094856142?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20161005095116106?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20161005095152006?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
Install Moq
![](http://img.blog.csdn.net/20161005095242648?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20161005095330967?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
Prepare Code:
Add /Abstract/IAuctionRepository.cs
Add /Entities/Auction.cs
Add /Entities/Auctioneer.cs
Add /Entities/Winner.cs
After the installation of Ninject, a file /App_Start/NinjectWebCommon.cs will be generated, change its RegisterServices() method as:
Add a controller:
Install Moq
Prepare Code:
Add /Abstract/IAuctionRepository.cs
using System.Collections.Generic; using TeAwaOnlineArtworkAuction.Entities; namespace TeAwaOnlineArtworkAuction.Abstract { public interface IAuctionsRepository { IEnumerable<Auction> Auctions { get; } IEnumerable<Winner> Winners { get; } IEnumerable<Auctioneer> Auctioneers { get; } } }
Add /Entities/Auction.cs
using System; namespace TeAwaOnlineArtworkAuction.Entities { public class Auction { public int Id { get; set; } public int UserId { get; set; } public string User { get; set; } public string Category { get; set; } public int Bid { get; set; } public string Title { get; set; } public string Description { get; set; } public string PhotoLink { get; set; } public DateTime StartingDate { get; set; } public DateTime EndingDate { get; set; } public decimal StartingPrice { get; set; } public decimal ReservePrice { get; set; } public decimal CurrentBid { get; set; } public decimal CurrentFee { get; set; } public int CountOfBids { get; set; } public char Sold { get; set; } } }
Add /Entities/Auctioneer.cs
namespace TeAwaOnlineArtworkAuction.Entities { public class Auctioneer { public int Id { get; set; } public string Email { get; set; } public string NickName { get; set; } } }
Add /Entities/Winner.cs
namespace TeAwaOnlineArtworkAuction.Entities { public class Winner { public int Id { get; set; } public int AuctionId { get; set; } public int WinnerId { get; set; } public decimal BidAmount { get; set; } public bool Paid { get; set; } } }
After the installation of Ninject, a file /App_Start/NinjectWebCommon.cs will be generated, change its RegisterServices() method as:
using Moq; //... using TeAwaOnlineArtworkAuction.Abstract; using TeAwaOnlineArtworkAuction.Entities; //... private static void RegisterServices(IKernel kernel) { Mock<IAuctionsRepository> mock = new Mock<IAuctionsRepository>(); mock.Setup(m => m.Auctions).Returns( new List<Auction> { new Auction { Id = 1, UserId = 1, User ="David", Category="Painting", Title ="Water Color Painting - Face Outside The Window", Description = "A li A li Ali Ya", PhotoLink = "mock/2014-Spring-Face Outside-The-Window_medium_meitu_1000px.jpg", StartingPrice = 3000.00m, ReservePrice = 5000.00m, StartingDate = new DateTime(2016, 09, 20, 20, 34, 20), EndingDate = new DateTime(2016, 09, 30, 16, 0, 0), CurrentBid = 3200.00m, CountOfBids = 4 }, new Auction { Id= 2, UserId = 1, User ="David", Category="Painting", Title ="Water Color Painting - Easter Island", Description = "A li A li Ali Ya", PhotoLink = "mock/2015-11-17-Easter-Island_medium_meitu_1000px.jpg", StartingPrice = 3000.00m, ReservePrice = 5000.00m, StartingDate = new DateTime(2016, 09, 20, 20, 34, 20), EndingDate = new DateTime(2016, 09, 30, 16, 0, 0), CurrentBid = 3200.00m, CountOfBids = 4 }, new Auction { Id = 3, UserId = 1, User ="David", Category="Painting", Title ="Water Color Painting - Capturing A Fish", Description = "A li A li Ali Ya", PhotoLink = "mock/2015-11-18-Capturing-A-Fish_medium_meitu_1000px.jpg", StartingPrice = 3000.00m, ReservePrice = 5000.00m, StartingDate = new DateTime(2016, 09, 20, 20, 34, 20), EndingDate = new DateTime(2016, 09, 30, 16, 0, 0), CurrentBid = 3200.00m, CountOfBids = 4 } } ); mock.Setup(m => m.Winners).Returns( new List<Winner> { new Winner { Id = 1, AuctionId = 1, WinnerId = 1, BidAmount = 3000.00m } } ); mock.Setup(m => m.Auctioneers).Returns( new List<Auctioneer> { new Auctioneer { Id = 1, Email = "guru@hotmail.com", NickName = "stoya" } } ); kernel.Bind<IAuctionsRepository>().ToConstant(mock.Object); }
Add a controller:
namespace TeAwaOnlineArtworkAuction.Controllers { public class AuctionController : Controller { private readonly IAuctionsRepository repository; public AuctionController(IAuctionsRepository auctionRepo) { this.repository = auctionRepo; } // GET: Auction public ViewResult Index() { return View(repository.Auctions); } } }
相关文章推荐
- Joe Celko's Data and Databases: Concepts in Practice
- PH FIN want to have PR/PO in TWD and SGD but do invoice matching and payment in USD
- Urls, Routing and Area in Asp.net MVC
- ASP.NET MVC 4 - Layout and Section in Razor
- Code-First Migration and Extending Identity Accounts in ASP.NET MVC 5 and Visual Studio 2013
- New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
- The flow of MVC in Spring -- Part I -- Dispatcher and Controller
- Plug-In Hybrids: ASP.NET WebForms and ASP.MVC and ASP.NET Dynamic Data Side By Side
- RenderBody, RenderPage and RenderSection methods in MVC 3
- History of Best Paper Awards in AI, NLP, CV, PR, and ML
- Joe Celko's Trees and Hierarchies in SQL for Smarties
- How to use JSon data in mvc action and post form data use JQuery ajax
- ASP.NET WebForms and MVC together in one project
- Cache-and-Collect Lifecycle Management in Ninject 2.0
- Mock HtmlHelper use Moq works in mvc 3
- Leveraging .NET Components and IDE Integration: UI AOP in an MVC use case By Daniel Cazzulino [XML MVP]
- MVC architecture in ASP.Net using C# and Microsoft Data Access Application block
- using Silverlight 4 in an ASP.NET MVC 3 application and accessing data with JSON
- Authorize attribute and jquery AJAX in asp.net MVC
- Creating a Cascading Dropdown in ASP.net MVC 3 and jQuery (1)