Skip to content

Commit 9db1519

Browse files
Test using NUnit with Moq frameworks
1 parent 9832090 commit 9db1519

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using Moq;
3+
using NUnit.Framework;
4+
5+
namespace UnitTestingUsingNUnitWithMoq.Tests
6+
{
7+
[TestFixture]
8+
public class CreditDecisionTests
9+
{
10+
Mock<ICreditDecisionService> mockCreditDecisionService;
11+
12+
CreditDecision systemUnderTest;
13+
14+
[TestCase(100, "Declined")]
15+
[TestCase(549, "Declined")]
16+
[TestCase(550, "Maybe")]
17+
[TestCase(674, "Maybe")]
18+
[TestCase(675, "We look forward to doing business with you!")]
19+
public void MakeCreditDecision_Always_ReturnsExpectedResult(int creditScore, string expectedResult)
20+
{
21+
mockCreditDecisionService = new Mock<ICreditDecisionService>(MockBehavior.Strict);
22+
mockCreditDecisionService.Setup(p => p.GetCreditDecision(creditScore)).Returns(expectedResult);
23+
24+
systemUnderTest = new CreditDecision(mockCreditDecisionService.Object);
25+
var result = systemUnderTest.MakeCreditDecision(creditScore);
26+
27+
Assert.That(result, Is.EqualTo(expectedResult));
28+
29+
mockCreditDecisionService.VerifyAll();
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("UnitTestingUsingNUnitWithMoq.Tests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Microsoft")]
12+
[assembly: AssemblyProduct("UnitTestingUsingNUnitWithMoq.Tests")]
13+
[assembly: AssemblyCopyright("Copyright © Microsoft 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("bca37e2b-fd0a-425e-a2df-aa8c5d3faf28")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\packages\NUnit3TestAdapter.3.13.0\build\net35\NUnit3TestAdapter.props" Condition="Exists('..\packages\NUnit3TestAdapter.3.13.0\build\net35\NUnit3TestAdapter.props')" />
4+
<Import Project="..\packages\NUnit.3.12.0\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" />
5+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
6+
<PropertyGroup>
7+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
8+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
9+
<ProjectGuid>{BCA37E2B-FD0A-425E-A2DF-AA8C5D3FAF28}</ProjectGuid>
10+
<OutputType>Library</OutputType>
11+
<AppDesignerFolder>Properties</AppDesignerFolder>
12+
<RootNamespace>UnitTestingUsingNUnitWithMoq.Tests</RootNamespace>
13+
<AssemblyName>UnitTestingUsingNUnitWithMoq.Tests</AssemblyName>
14+
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
15+
<FileAlignment>512</FileAlignment>
16+
<Deterministic>true</Deterministic>
17+
<NuGetPackageImportStamp>
18+
</NuGetPackageImportStamp>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Castle.Core.4.4.0\lib\net45\Castle.Core.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Moq, Version=4.11.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Moq.4.11.0\lib\net45\Moq.dll</HintPath>
43+
</Reference>
44+
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
45+
<HintPath>..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll</HintPath>
46+
</Reference>
47+
<Reference Include="System" />
48+
<Reference Include="System.Configuration" />
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
51+
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
52+
</Reference>
53+
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
54+
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
55+
</Reference>
56+
<Reference Include="System.Xml.Linq" />
57+
<Reference Include="System.Data.DataSetExtensions" />
58+
<Reference Include="Microsoft.CSharp" />
59+
<Reference Include="System.Data" />
60+
<Reference Include="System.Net.Http" />
61+
<Reference Include="System.Xml" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<Compile Include="CreditDecisionTests.cs" />
65+
<Compile Include="Properties\AssemblyInfo.cs" />
66+
</ItemGroup>
67+
<ItemGroup>
68+
<None Include="packages.config" />
69+
</ItemGroup>
70+
<ItemGroup>
71+
<ProjectReference Include="..\UnitTestingUsingNUnitWithMoq\UnitTestingUsingNUnitWithMoq.csproj">
72+
<Project>{a0a8558e-d744-4ba4-8dbc-d877ce4525a9}</Project>
73+
<Name>UnitTestingUsingNUnitWithMoq</Name>
74+
</ProjectReference>
75+
</ItemGroup>
76+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
77+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
78+
<PropertyGroup>
79+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
80+
</PropertyGroup>
81+
<Error Condition="!Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.12.0\build\NUnit.props'))" />
82+
<Error Condition="!Exists('..\packages\NUnit3TestAdapter.3.13.0\build\net35\NUnit3TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit3TestAdapter.3.13.0\build\net35\NUnit3TestAdapter.props'))" />
83+
</Target>
84+
</Project>

Diff for: UnitTestingUsingNUnitWithMoq.Tests/packages.config

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Castle.Core" version="4.4.0" targetFramework="net47" />
4+
<package id="Moq" version="4.11.0" targetFramework="net47" />
5+
<package id="NUnit" version="3.12.0" targetFramework="net47" />
6+
<package id="NUnit3TestAdapter" version="3.13.0" targetFramework="net47" />
7+
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net47" />
8+
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net47" />
9+
</packages>

Diff for: UnitTestingUsingNUnitWithMoq.sln

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.28307.539
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingUsingNUnitWithMoq", "UnitTestingUsingNUnitWithMoq\UnitTestingUsingNUnitWithMoq.csproj", "{A0A8558E-D744-4BA4-8DBC-D877CE4525A9}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestingUsingNUnitWithMoq.Tests", "UnitTestingUsingNUnitWithMoq.Tests\UnitTestingUsingNUnitWithMoq.Tests.csproj", "{BCA37E2B-FD0A-425E-A2DF-AA8C5D3FAF28}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{A0A8558E-D744-4BA4-8DBC-D877CE4525A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{A0A8558E-D744-4BA4-8DBC-D877CE4525A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{A0A8558E-D744-4BA4-8DBC-D877CE4525A9}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{BCA37E2B-FD0A-425E-A2DF-AA8C5D3FAF28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{BCA37E2B-FD0A-425E-A2DF-AA8C5D3FAF28}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{BCA37E2B-FD0A-425E-A2DF-AA8C5D3FAF28}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{BCA37E2B-FD0A-425E-A2DF-AA8C5D3FAF28}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)