Set Up Ninject and Moq In A MVC 5 Prjoect
来源:互联网 发布:淘宝蹭淘气值评价 编辑:程序博客网 时间:2024/06/01 03:59
Install Ninject
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); }}}
0 0
- Set Up Ninject and Moq In A MVC 5 Prjoect
- Set up and run a PBS job
- How to set up a simple mail server on Debian in 5 easy steps
- Set up a flag
- Moving items up and down in a ListView control
- How to set up and test a simple OLEDB Linked Server in Microsoft® SQL Server to allow retrieval of d
- Set up axis2 in Jboss
- Set up Project in BF
- Set up SVN in Ubuntu
- Hadoop install and set up
- Mixture of conda2 and conda3 problem and A guidance of environment set-up for windows users
- Qt creator needs a compiler set up to build. Configure a compiler in the kit options
- ALV in a Pop up window and ALV in a dialog box
- set up ip address in redhat el 5
- How to set up usblan and telnet for A780 in 3 easy steps
- Set up the Virtual switch and Virtual adapter in Windows 2012R2
- How To Set Up A Serial Port Between Two Virtual Machines In VirtualBox
- vulakn教程--Drawing a Triangle--Set up--Physical Device and Queue Family
- java中的接口为什么可以多继承,而其他类则不能实现多继承
- 51nod-1670 打怪兽
- 计算机网络常见面试题总结
- 关于STL中vector使用unique()去重的问题
- 文章标题
- Set Up Ninject and Moq In A MVC 5 Prjoect
- vc++包管理工具
- 理解 heap --- 实现一个简单的 malloc
- Hadoop简介
- Android笔记之飞行模式
- 第一次任务小计(一)
- Word Ladder I
- poj1847Tram
- blog, microblog, wechat