Skip to content

Commit

Permalink
First cut of testing extensions
Browse files Browse the repository at this point in the history
Adds async enumerable helper classes to EntityFramework.Testing

Adds a MockDebSet<T> to EntityFramework.Testing.Moq and the ability to
specify seed data and setup LINQ
  • Loading branch information
rowanmiller committed May 12, 2014
1 parent 036f90f commit c24c272
Show file tree
Hide file tree
Showing 24 changed files with 932 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .nuget/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NuGet.CommandLine" version="2.8.1" />
</packages>
2 changes: 2 additions & 0 deletions BuildPackages.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages\NuGet.CommandLine.2.8.1\tools\NuGet.exe pack src\EntityFramework.Testing\EntityFramework.Testing.csproj
packages\NuGet.CommandLine.2.8.1\tools\NuGet.exe pack src\EntityFramework.Testing.Moq\EntityFramework.Testing.Moq.csproj
48 changes: 48 additions & 0 deletions EntityFramework.Testing.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30324.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFramework.Testing", "src\EntityFramework.Testing\EntityFramework.Testing.csproj", "{4351E4D4-C68C-4CF6-998B-AB56D8A3760D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFramework.Testing.Moq", "src\EntityFramework.Testing.Moq\EntityFramework.Testing.Moq.csproj", "{13B2C70C-A084-44AF-8F75-975D84438DEE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{56EF6CEF-D305-42B5-B0FC-1528B670548E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{7532A630-3026-4E1A-9E5C-13611C22C5F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFramework.Testing.Moq.Tests", "test\EntityFramework.Testing.Moq.Tests\EntityFramework.Testing.Moq.Tests.csproj", "{1F55F581-F0D8-423D-A514-636C161ACDB4}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{A5D7A40E-6C6E-4119-A6FA-861C176A3F2A}"
ProjectSection(SolutionItems) = preProject
.nuget\packages.config = .nuget\packages.config
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4351E4D4-C68C-4CF6-998B-AB56D8A3760D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4351E4D4-C68C-4CF6-998B-AB56D8A3760D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4351E4D4-C68C-4CF6-998B-AB56D8A3760D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4351E4D4-C68C-4CF6-998B-AB56D8A3760D}.Release|Any CPU.Build.0 = Release|Any CPU
{13B2C70C-A084-44AF-8F75-975D84438DEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13B2C70C-A084-44AF-8F75-975D84438DEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13B2C70C-A084-44AF-8F75-975D84438DEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13B2C70C-A084-44AF-8F75-975D84438DEE}.Release|Any CPU.Build.0 = Release|Any CPU
{1F55F581-F0D8-423D-A514-636C161ACDB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F55F581-F0D8-423D-A514-636C161ACDB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F55F581-F0D8-423D-A514-636C161ACDB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F55F581-F0D8-423D-A514-636C161ACDB4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4351E4D4-C68C-4CF6-998B-AB56D8A3760D} = {56EF6CEF-D305-42B5-B0FC-1528B670548E}
{13B2C70C-A084-44AF-8F75-975D84438DEE} = {56EF6CEF-D305-42B5-B0FC-1528B670548E}
{1F55F581-F0D8-423D-A514-636C161ACDB4} = {7532A630-3026-4E1A-9E5C-13611C22C5F6}
EndGlobalSection
EndGlobal
13 changes: 13 additions & 0 deletions src/EntityFramework.Testing.Moq/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
77 changes: 77 additions & 0 deletions src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{13B2C70C-A084-44AF-8F75-975D84438DEE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EntityFramework.Testing.Moq</RootNamespace>
<AssemblyName>EntityFramework.Testing.Moq</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MockDbSetExtenstions.cs" />
<Compile Include="MockDbSet`.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="EntityFramework.Testing.Moq.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EntityFramework.Testing\EntityFramework.Testing.csproj">
<Project>{4351e4d4-c68c-4cf6-998b-ab56d8a3760d}</Project>
<Name>EntityFramework.Testing</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
19 changes: 19 additions & 0 deletions src/EntityFramework.Testing.Moq/EntityFramework.Testing.Moq.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>1.0.0-apha1</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<licenseUrl>https://github.com/rowanmiller/EntityFramework.Testing/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/rowanmiller/EntityFramework.Testing</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>Copyright 2014</copyright>
<tags>Entity Framework EF EF6 Testing</tags>
<dependencies>
<dependency id="EntityFramework.Testing" version="$version$" />
</dependencies>
</metadata>
</package>
53 changes: 53 additions & 0 deletions src/EntityFramework.Testing.Moq/MockDbSetExtenstions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Moq;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;

namespace EntityFramework.Testing.Moq
{
public static class MockDbSetExtenstions
{
public static MockDbSet<TEntity> SetupSeedData<TEntity>(
this MockDbSet<TEntity> set,
IEnumerable<TEntity> data)
where TEntity : class
{
set.AddData(data);

// Need to re-setup LINQ if the data changes
if(set.IsLinqSetup)
{
set.SetupLinq();
}

return set;
}

public static MockDbSet<TEntity> SetupLinq<TEntity>(this MockDbSet<TEntity> set)
where TEntity : class
{
// Record so that we can re-setup linq if the data is changed
set.IsLinqSetup = true;

// Enable direct async enumeration of set
set.As<IDbAsyncEnumerable<TEntity>>()
.Setup(m => m.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator<TEntity>(set.Queryable.GetEnumerator()));

// Enable LINQ queries with async enumeration
set.As<IQueryable<TEntity>>()
.Setup(m => m.Provider)
.Returns(new TestDbAsyncQueryProvider<TEntity>(set.Queryable.Provider));

// Wire up LINQ provider to fall back to in memory LINQ provider of the data
set.As<IQueryable<TEntity>>().Setup(m => m.Expression).Returns(set.Queryable.Expression);
set.As<IQueryable<TEntity>>().Setup(m => m.ElementType).Returns(set.Queryable.ElementType);
set.As<IQueryable<TEntity>>().Setup(m => m.GetEnumerator()).Returns(set.Queryable.GetEnumerator());

// Enable Include directly on the DbSet (Include extension method on IQueryable is a no-op when it's not a DbSet/DbQuery)
// Include(string) and Include(Func<TEntity, TProperty) both fall back to string
set.Setup(s => s.Include(It.IsAny<string>())).Returns(set.Object);
return set;
}
}
}
37 changes: 37 additions & 0 deletions src/EntityFramework.Testing.Moq/MockDbSet`.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Moq;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace EntityFramework.Testing.Moq
{
public class MockDbSet<TEntity> : Mock<DbSet<TEntity>>
where TEntity : class
{
private IQueryable<TEntity> _queryable;
private List<TEntity> _data;

public MockDbSet()
{
_data = new List<TEntity>();
_queryable = _data.AsQueryable();
}

public IEnumerable<TEntity> Data
{
get { return _data; }
}

internal bool IsLinqSetup { get; set; }

internal IQueryable<TEntity> Queryable
{
get { return _queryable; }
}

internal void AddData(IEnumerable<TEntity> data)
{
_data.AddRange(data);
}
}
}
36 changes: 36 additions & 0 deletions src/EntityFramework.Testing.Moq/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("EntityFramework.Testing.Moq")]
[assembly: AssemblyDescription("Testing helpers for using Moq with Entity Framework")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Rowan Miller")]
[assembly: AssemblyProduct("EntityFramework.Testing.Moq")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("43b16b3a-167f-4961-84dc-3545ad860de8")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
5 changes: 5 additions & 0 deletions src/EntityFramework.Testing.Moq/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.0.0" targetFramework="net45" />
<package id="Moq" version="4.2.1402.2112" targetFramework="net45" />
</packages>
13 changes: 13 additions & 0 deletions src/EntityFramework.Testing/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
69 changes: 69 additions & 0 deletions src/EntityFramework.Testing/EntityFramework.Testing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{4351E4D4-C68C-4CF6-998B-AB56D8A3760D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EntityFramework.Testing</RootNamespace>
<AssemblyName>EntityFramework.Testing</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="TestDbAsyncEnumerable`.cs" />
<Compile Include="TestDbAsyncEnumerator`.cs" />
<Compile Include="TestDbAsyncQueryProvider`.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="EntityFramework.Testing.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit c24c272

Please sign in to comment.