7.2.2 Making a Mock Repository; e.g. 7.5

chap07
Jason Zhu 2021-08-20 12:41:27 +10:00
parent ece061c043
commit 91beed1a4e
5 changed files with 24 additions and 4 deletions

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace SportsStore.Domain.Abstract namespace SportsStore.Domain.Abstract
{ {
interface IProductsRepository public interface IProductRepository
{ {
IQueryable<Product> Products { get; } IQueryable<Product> Products { get; }
} }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace SportsStore.Domain.Entities namespace SportsStore.Domain.Entities
{ {
class Product public class Product
{ {
public int ProductID { get; set; } public int ProductID { get; set; }
public string Name { get; set; } public string Name { get; set; }

View File

@ -57,7 +57,9 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Abstract\IProductRepository.cs" />
<Compile Include="Class1.cs" /> <Compile Include="Class1.cs" />
<Compile Include="Entities\Product.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,10 +1,13 @@
using Ninject; using Moq;
using Ninject;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Web; using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
using System.Web.Routing; using System.Web.Routing;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
namespace SportsStore.WebUI.Infrastructure namespace SportsStore.WebUI.Infrastructure
{ {
@ -26,7 +29,16 @@ namespace SportsStore.WebUI.Infrastructure
private void AddBindings() private void AddBindings()
{ {
// put additional bindings here // Mock implementation of the IProductRepository Interface
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new List<Product>
{
new Product { Name = "Football", Price = 25 },
new Product { Name = "Surf board", Price = 179 },
new Product { Name = "Running shoes", Price = 95 }
}.AsQueryable());
ninjectKernel.Bind<IProductRepository>().ToConstant(mock.Object);
} }
} }
} }

View File

@ -196,6 +196,12 @@
<Content Include="Scripts\jquery-3.4.1.slim.min.map" /> <Content Include="Scripts\jquery-3.4.1.slim.min.map" />
<Content Include="Scripts\jquery-3.4.1.min.map" /> <Content Include="Scripts\jquery-3.4.1.min.map" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SportsStore.Domain\SportsStore.Domain.csproj">
<Project>{cfedebcd-de26-4a14-8cf2-95f735b1bb12}</Project>
<Name>SportsStore.Domain</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>