Skip to content

Commit

Permalink
Example of how to unit test a controller method which supports odata …
Browse files Browse the repository at this point in the history
…protocol
  • Loading branch information
[email protected] committed Jun 27, 2021
1 parent c00cfa1 commit a679422
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
12 changes: 6 additions & 6 deletions AspNetCoreOData.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ODataDynamicModel", "sample
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ODataSampleCommon", "sample\ODataSampleCommon\ODataSampleCommon.csproj", "{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ODataRoutingSample.Tests", "sample\ODataRoutingSample.Tests\ODataRoutingSample.Tests.csproj", "{CD0608D8-36D0-4E45-9355-A2040960ADC8}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ODataRoutingSample.Tests", "sample\ODataRoutingSample.Tests\ODataRoutingSample.Tests.csproj", "{2084B1F8-5122-41A1-8735-06F36E7C9903}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -65,10 +65,10 @@ Global
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA}.Release|Any CPU.Build.0 = Release|Any CPU
{CD0608D8-36D0-4E45-9355-A2040960ADC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD0608D8-36D0-4E45-9355-A2040960ADC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD0608D8-36D0-4E45-9355-A2040960ADC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD0608D8-36D0-4E45-9355-A2040960ADC8}.Release|Any CPU.Build.0 = Release|Any CPU
{2084B1F8-5122-41A1-8735-06F36E7C9903}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2084B1F8-5122-41A1-8735-06F36E7C9903}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2084B1F8-5122-41A1-8735-06F36E7C9903}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2084B1F8-5122-41A1-8735-06F36E7C9903}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -82,7 +82,7 @@ Global
{BDC5474B-9511-4CDF-83FE-376C7130F7F0} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{CE04E38B-547F-46C0-ABE4-F981E3A1874F} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{647EFCFA-55A7-4F0A-AD40-4B6EB1BFCFFA} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{CD0608D8-36D0-4E45-9355-A2040960ADC8} = {B1F86961-6958-4617-ACA4-C231F95AE099}
{2084B1F8-5122-41A1-8735-06F36E7C9903} = {B1F86961-6958-4617-ACA4-C231F95AE099}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {540C9752-AAC0-49EA-BA60-78490C90FF86}
Expand Down
42 changes: 34 additions & 8 deletions sample/ODataRoutingSample.Tests/AccountsControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -8,29 +7,27 @@
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;
using Microsoft.OData.UriParser;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ODataRoutingSample.Controllers;
using ODataRoutingSample.Models;
using Xunit;

namespace ODataRoutingSample.Tests
{
/// <summary>
/// This is a suggestion how to unit test a controller which will be very useful for the devs
/// </summary>
[TestClass]
public class AccountsControllerTests
{
// It is not mocked because it is not used
private readonly ILogger<WeatherForecastController> _mockedLogger = null;
private AccountsController _accountsController;

[TestInitialize]
public void TestInit()
public AccountsControllerTests()
{
_accountsController = new AccountsController(_mockedLogger);
}

[TestMethod]
[Fact]
public void AccountsController_GetTopTwoAccounts_ShouldReturnTopTwoAccounts()
{
// Arrange
Expand All @@ -43,7 +40,7 @@ public void AccountsController_GetTopTwoAccounts_ShouldReturnTopTwoAccounts()
options.AddModel("my{data}", edmModel);

HttpRequest request = RequestFactory.Create("GET",
"http://localhost/api?$top=2&$inlinecount=allpages",
"http://localhost/api?$top=2&$count=true",
dataOptions => dataOptions.AddModel("odata", edmModel));

var oDataQueryContext = new ODataQueryContext(edmModel, typeof(Account), new ODataPath());
Expand All @@ -56,7 +53,36 @@ public void AccountsController_GetTopTwoAccounts_ShouldReturnTopTwoAccounts()
var accounts = (IQueryable<Account>) ((OkObjectResult) result).Value;

// Assert
Assert.AreEqual(2, accounts.Count(), "Incorrect returned number of top query!");
Assert.Equal(2, accounts.Count());
}

[Fact]
public void AccountsController_SelectAccountWithNameEqualHot_ShouldReturnAccountWithHotName()
{
// Arrange
var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Account>("Account");
var edmModel = modelBuilder.GetEdmModel();

var options = new ODataOptions();
options.AddModel("odata", EdmCoreModel.Instance);
options.AddModel("my{data}", edmModel);

HttpRequest request = RequestFactory.Create("GET",
"http://localhost/api?filter=Name eq 'Hot'",
dataOptions => dataOptions.AddModel("odata", edmModel));

var oDataQueryContext = new ODataQueryContext(edmModel, typeof(Account), new ODataPath());

var aDataQueryOptions = new ODataQueryOptions<Account>(oDataQueryContext, request);

// Act
_accountsController = new AccountsController(_mockedLogger);
var result = _accountsController.Get(aDataQueryOptions);
var accounts = (IQueryable<Account>)((OkObjectResult)result).Value;

// Assert
Assert.Equal("Hot", accounts.FirstOrDefault()?.Name);
}
}
}
12 changes: 9 additions & 3 deletions sample/ODataRoutingSample.Tests/ODataRoutingSample.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit a679422

Please sign in to comment.