-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add first draft of Azure Functions unit tests
- Loading branch information
1 parent
7eef072
commit 3f64599
Showing
5 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
samples/AzureFunctionsUnitTests/AzureFunctionsUnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
<PackageReference Include="moq" Version="4.20.70" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\AzureFunctionsApp\AzureFunctionsApp.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace Company.Function; // same namespace as the Azure Functions app | ||
|
||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.DurableTask; | ||
using Moq; | ||
|
||
public class SampleUnitTests | ||
{ | ||
[Fact] | ||
public async Task OrchestrationReturnsMultipleGreetings() | ||
{ | ||
// create mock orchestration context, and mock ILogger. | ||
var contextMock = new Mock<TaskOrchestrationContext>(); | ||
|
||
// mock activity results | ||
// In Moq, optional arguments need to be specified as well. We specify them with It.IsAny<T>(), where T is the type of the optional argument | ||
contextMock.Setup(x => x.CallActivityAsync<string>(nameof(AzureFunctionsApp.SayHello), "Tokyo", It.IsAny<TaskOptions>())) | ||
.ReturnsAsync("Hello Tokyo!"); | ||
contextMock.Setup(x => x.CallActivityAsync<string>(nameof(AzureFunctionsApp.SayHello), "Seattle", It.IsAny<TaskOptions>())) | ||
.ReturnsAsync("Hello Seattle!"); | ||
contextMock.Setup(x => x.CallActivityAsync<string>(nameof(AzureFunctionsApp.SayHello), "London", It.IsAny<TaskOptions>())) | ||
.ReturnsAsync("Hello London!"); | ||
|
||
// execute the orchestrator | ||
var contextObj = contextMock.Object; | ||
List<string> outputs = await AzureFunctionsApp.RunOrchestrator(contextObj); | ||
|
||
// assert expected outputs | ||
Assert.Equal(3, outputs.Count); | ||
Assert.Equal("Hello Tokyo!", outputs[0]); | ||
Assert.Equal("Hello Seattle!", outputs[1]); | ||
Assert.Equal("Hello London!", outputs[2]); | ||
} | ||
|
||
[Fact] | ||
public void ActivityReturnsGreeting() | ||
{ | ||
var contextMock = new Mock<FunctionContext>(); | ||
var context = contextMock.Object; | ||
|
||
string output = AzureFunctionsApp.SayHello("Tokyo", context); | ||
|
||
// assert expected outputs | ||
Assert.Equal("Hello Tokyo!", output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters