Skip to content

[Feature]: Add first-class support for classes like Azure SDK concrete clients in TUnit.Mocks #5308

@dukedagmor

Description

@dukedagmor

Problem Statement

Findings

When migrating from Moq to Tunit.Mocks I encountered the following issue. Since this isn't supported yet, I made this a feature request and not a bug.

Mocking concrete Azure.Data.Tables.TableClient is not supported in Tunit but is in Moq. This complicates the migration.

For now we use Tunit.Mocks 'where possible'

Reproduction

#:property LangVersion=preview
#:property ManagePackageVersionsCentrally=false
#:package TUnit.Mocks@1.21.30
#:package Azure.Data.Tables@12.11.0
#:package Azure.Storage.Blobs@12.27.0

using Azure.Data.Tables;
using Azure.Storage.Blobs;

var tableClientMock = Mock.OfPartial<TableClient>(MockBehavior.Strict);
var blobClientMock = Mock.OfPartial<BlobClient>(MockBehavior.Strict);

Console.WriteLine($"Created: {tableClientMock.Object.GetType().Name}, {blobClientMock.Object.GetType().Name}");
dotnet run .\test-compat\tunit-mocks-azure-concrete-repro.cs

Expected result

The build fails in TUnit.Mocks generated files when mocking Azure concrete clients (TableClient, BlobClient), with errors like:

  • CS0460 (constraints on overrides)
  • CS0115 (no suitable method to override)
  • CS0122 (inaccessible members)

Proposed Solution

Idealy it would work like moq does now, that decreases friction when migrating. If this is wholly undesired a decision needs to be made if support + warning is valid or no support at all is better to keep people from implementing this as path of least resistance.

moq example

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Moq" Version="4.20.72" />
    <PackageReference Include="Azure.Data.Tables" Version="12.11.0" />
    <PackageReference Include="Azure.Storage.Blobs" Version="12.27.0" />
  </ItemGroup>
</Project>
using Azure.Data.Tables;
using Azure.Storage.Blobs;
using Moq;

var tableClientMock = new Mock<TableClient>(MockBehavior.Strict);
var blobClientMock = new Mock<BlobClient>(MockBehavior.Strict);

Console.WriteLine($"Created: {tableClientMock.Object.GetType().Name}, {blobClientMock.Object.GetType().Name}");

Alternatives Considered

No response

Feature Category

Other

How important is this feature to you?

Nice to have - would improve my experience

Additional Context

No response

Contribution

  • I'm willing to submit a pull request for this feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions