Set Up Ninject and Moq In A MVC 5 Prjoect

17 篇文章 0 订阅
15 篇文章 0 订阅

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);
        }
	}
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值