Skip to content

Commit

Permalink
Fix mockContext for fixtures. (#34)
Browse files Browse the repository at this point in the history
* Add required packages to test project.

* Add assembly info for test project.

* Fix references for resource manager.

* Remove constructor overloading & fix mockContext, also fix references for resource manager

* Dispose the context in StorageAccountFixture and fix the mockContext.
  • Loading branch information
romahamu authored Mar 13, 2020
1 parent 02d241b commit 768ba77
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 39 deletions.
22 changes: 4 additions & 18 deletions src/StorageCache/HPCCache.Test/Fixtures/HpcCacheTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace Microsoft.Azure.Commands.HPCCache.Test.Fixtures
using Microsoft.Azure.Commands.HPCCache.Test.Utilities;
using Microsoft.Azure.Management.Authorization;
using Microsoft.Azure.Management.Authorization.Models;
using Microsoft.Azure.Management.Internal.Resources;
using Microsoft.Azure.Management.Internal.Resources.Models;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.StorageCache;
using Microsoft.Azure.Management.StorageCache.Models;
using Microsoft.Rest.Azure;
Expand All @@ -49,31 +49,17 @@ public class HpcCacheTestContext : IDisposable
/// </summary>
private bool disposedValue = false;

/// <summary>
/// Initializes a new instance of the <see cref="HpcCacheTestContext"/> class.
/// </summary>
/// <param name="suiteObject">Class object.</param>
/// <param name="methodName">Method name of the calling method.</param>
public HpcCacheTestContext(
object suiteObject,
[System.Runtime.CompilerServices.CallerMemberName]
string methodName = ".ctor")
{
this.mockContext = MockContext.Start(suiteObject.GetType().Name, methodName);
this.RegisterSubscriptionForResource("Microsoft.StorageCache");
}

/// <summary>
/// Initializes a new instance of the <see cref="HpcCacheTestContext"/> class.
/// </summary>
/// <param name="type">Class type.</param>
/// <param name="methodName">Method name of the calling method.</param>
public HpcCacheTestContext(
Type type,
string className,
[System.Runtime.CompilerServices.CallerMemberName]
string methodName = ".ctor")
{
this.mockContext = MockContext.Start(type.Name, methodName);
this.mockContext = MockContext.Start(className, methodName);
this.RegisterSubscriptionForResource("Microsoft.StorageCache");
}

Expand Down
11 changes: 8 additions & 3 deletions src/StorageCache/HPCCache.Test/Fixtures/HpcCacheTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
namespace Microsoft.Azure.Commands.HPCCache.Test.Fixtures
{
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.Azure.Commands.HPCCache.Test.Helper;
using Microsoft.Azure.Commands.HPCCache.Test.Utilities;
using Microsoft.Azure.Management.Internal.Resources.Models;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.StorageCache;
using Microsoft.Azure.Management.StorageCache.Models;
using Microsoft.Azure.Test.HttpRecorder;
Expand Down Expand Up @@ -59,9 +60,13 @@ public class HpcCacheTestFixture : IDisposable
/// </summary>
public HpcCacheTestFixture()
{
using (this.Context = new HpcCacheTestContext(this))
var sf = new StackTrace().GetFrame(1);
var callingClassType = sf.GetMethod().ReflectedType?.ToString();
var mockName = sf.GetMethod().Name;

using (this.Context = new HpcCacheTestContext(callingClassType, mockName))
{
this.Context = new HpcCacheTestContext(this);
//this.Context = new HpcCacheTestContext(this);
try
{
StorageCacheManagementClient storagecacheMgmtClient = this.Context.GetClient<StorageCacheManagementClient>();
Expand Down
18 changes: 11 additions & 7 deletions src/StorageCache/HPCCache.Test/Fixtures/StorageAccountFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.Azure.Commands.HPCCache.Test.Helper;
using Microsoft.Azure.Commands.HPCCache.Test.Utilities;
using Microsoft.Azure.Management.Internal.Resources.Models;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using Microsoft.Azure.Management.StorageCache;
Expand Down Expand Up @@ -46,9 +46,12 @@ public class StorageAccountFixture : IDisposable
public StorageAccountFixture(HpcCacheTestFixture fixture)
{
this.fixture = fixture;
using (this.Context = new HpcCacheTestContext(this))
var sf = new StackTrace().GetFrame(1);
var callingClassType = sf.GetMethod().ReflectedType?.ToString();
var mockName = sf.GetMethod().Name;
using (this.Context = new HpcCacheTestContext(callingClassType, mockName))
{
this.Context = new HpcCacheTestContext(this);
// this.Context = new HpcCacheTestContext(this);
this.StorageTarget = this.AddClfsStorageTarget(this.Context);
Match clfsTargetMatch = ClfsTargetRegex.Match(this.StorageTarget.Clfs.Target);
var storageAccountName = clfsTargetMatch.Groups["StorageAccountName"].Value;
Expand Down Expand Up @@ -77,11 +80,12 @@ public StorageAccountFixture(HpcCacheTestFixture fixture)
/// </summary>
public StorageTarget StorageTarget { get; set; }

/// <inheritdoc/>
/// <summary>
/// Dispose the object.
/// </summary>
public void Dispose()
{
// We do not create anything in this fixture but
// just use the HpcCacheTestFixture which has its own disposal method.
this.Context.Dispose();
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/StorageCache/HPCCache.Test/HPCCache.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.StorageCache" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HPCCache\HPCCache.csproj" />
<PackageReference Include="Microsoft.Azure.Management.Network" Version="19.18.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="13.1.0" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.0.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Authorization" Version="2.11.0-preview" />
</ItemGroup>

</Project>
6 changes: 1 addition & 5 deletions src/StorageCache/HPCCache.Test/Helper/CacheHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ namespace Microsoft.Azure.Commands.HPCCache.Test.Helper
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Commands.HPCCache.Test.Utilities;
using Microsoft.Azure.Management.Internal.Resources.Models;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.StorageCache;
using Microsoft.Azure.Management.StorageCache.Models;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.Azure;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Xunit.Abstractions;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Microsoft.Azure.Commands.HPCCache.Test.Helper
{
using Microsoft.Azure.Management.Internal.Resources.Models;
using Microsoft.Azure.Management.ResourceManager.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using Sku = Microsoft.Azure.Management.Storage.Models.Sku;
Expand Down
50 changes: 50 additions & 0 deletions src/StorageCache/HPCCache.Test/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Reflection;
using System.Runtime.InteropServices;
using Xunit;

// 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("Microsoft.Azure.Commands.HPCCache.Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Microsoft.Azure.Commands.HPCCache.Test")]
[assembly: AssemblyCopyright(Microsoft.WindowsAzure.Commands.Common.AzurePowerShell.AssemblyCopyright)]
[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("4a088025-58a6-4864-99ca-07a1e37cb734")]

// 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.0")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: CollectionBehavior(DisableTestParallelization = true)]
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Microsoft.Azure.Commands.HPCCache.Test.ScenarioTests
using System.Linq;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.HPCCache.Test.Fixtures;
using Microsoft.Azure.Management.Internal.Resources;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.StorageCache;
using Microsoft.Azure.ServiceManagement.Common.Models;
Expand Down

0 comments on commit 768ba77

Please sign in to comment.