Azure功能单元测试凭据

时间:2017-10-31 11:04:25

标签: unit-testing azure azure-storage azure-functions

我在Visual Studio中有一个简单的Azure Functions应用程序。我正在尝试对创建游戏功能进行单元测试,该功能应该在本地保存数据。

我试图在Visual Studio中运行函数CreateGameTest作为测试,我收到以下输出

[31/10/2017 10:36:46 Informational] ------ Run test started ------
[31/10/2017 10:36:47 Informational] [xUnit.net 00:00:00.2509695]   Starting:    LemonadeGame.Test
[31/10/2017 10:36:47 Error] [xUnit.net 00:00:00.7378276]     LemonadeGame.Test.CreateGameTest.TestDoesCreateGame [FAIL]
[31/10/2017 10:36:47 Informational] [xUnit.net 00:00:00.7392995]       System.InvalidOperationException : Cannot find account information inside Uri 'http://127.0.0.1:10002/devstoreaccount1'
[31/10/2017 10:36:47 Informational] [xUnit.net 00:00:00.7403799]       Stack Trace:
[31/10/2017 10:36:47 Informational] [xUnit.net 00:00:00.7408970]            at Microsoft.WindowsAzure.Storage.Core.Util.NavigationHelper.GetContainerNameFromContainerAddress(Uri uri, Nullable`1 usePathStyleUris)
[31/10/2017 10:36:47 Informational] [xUnit.net 00:00:00.7413043]            at Microsoft.WindowsAzure.Storage.Table.CloudTable.ParseQueryAndVerify(StorageUri address, StorageCredentials credentials)
[31/10/2017 10:36:47 Informational] [xUnit.net 00:00:00.7418036]            at Microsoft.WindowsAzure.Storage.Table.CloudTable..ctor(StorageUri tableAddress, StorageCredentials credentials)
[31/10/2017 10:36:47 Informational] [xUnit.net 00:00:00.7421637]         C:\Users\Ben Helstrip\Documents\Visual Studio 2017\azure-server-lemonade-game\LemonadeGame\LemonadeGame.Test\CreateGameTest.cs(46,0): at LemonadeGame.Test.CreateGameTest.TestDoesCreateGame()
[31/10/2017 10:36:47 Informational] [xUnit.net 00:00:00.7505642]   Finished:    LemonadeGame.Test
[31/10/2017 10:36:47 Informational] ========== Run test finished: 1 run (0:00:00.7973298) ==========

从这看来,我错过了一些帐户信息,无法在本地运行,但我在指定用于本地开发的devstoreaccount帐户名和Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==帐户密钥。

运行此测试还有其他必须包含的内容吗?

我的单元测试

using System;
using System.Net;
using System.Net.Http;
using System.Web.Http.Results;
using LemonadeGame.Models;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Table;
using Should.Fluent;
using Xunit;

namespace LemonadeGame.Test
{
    public class CreateGameTest
    {
        static void Main(string[] args)
        {
            var game = new Game
            {
                GameName = "Ben.Helstrip",
                NumberOfPlayers = 5,
                AutoNextRound = false,
                PartitionKey = "game",
                Timestamp = DateTimeOffset.Now
            };
        }
        [Fact]
        public void TestDoesCreateGame()
        {
            var request = new HttpRequestMessage
            {
                Content = new StringContent("{'name': 'Bill'}", System.Text.Encoding.UTF8, "application/json"),
            };
            request.SetConfiguration(new System.Web.Http.HttpConfiguration());
            var storageCredentials = new StorageCredentials("devstoreaccount1", "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==");
            var storageUri = new StorageUri(new Uri("http://127.0.0.1:10002/devstoreaccount1"));
            var game = new Game
            {
                GameName = "Ben",
                NumberOfPlayers = 5,
                AutoNextRound = false,
                PartitionKey = "game",
                Timestamp = DateTimeOffset.Now
            };

            var result = FunctionCreateGame.Run(game, new CloudTable(storageUri, storageCredentials),  new FakeTraceWriter());
            result.StatusCode.Should().Equal(HttpStatusCode.Accepted);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

传递给Uri ctor的CloudTable需要是表格的Uri,而不是存储帐户的基础RewriteRule ^moviename.html songs.php?lan=3&movie-123 RewriteRule ^another-moviename.html songs.php?lan=4&movie-124

请参阅那里的CloudTable文档。本文还讨论了如何使用存储模拟器。 https://docs.microsoft.com/en-us/azure/storage/common/storage-use-emulator