您的位置:首页 > 其它

Set Up Ninject and Moq In A MVC 5 Prjoect

2016-10-05 10:28 204 查看
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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ninject moq mvc