jaguar68
jaguar68

Reputation: 179

Visual Studio 2022 - Add System.Runtime fake assembly

Simple test project: xUnit, targeting Net 6.0 Environment:

Add fake assembly of System.Runtime, compilation errors:

1>------ Rebuild All started: Project: TestProject1, Configuration: Debug Any CPU ------
Restored D:\TestProject1\TestProject1\TestProject1.csproj (in 570 ms).
1>D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.cs(94904,16): error CS1503: Argument 1: cannot convert from 'string' to 'System.Runtime.Serialization.SerializationInfo' [D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.csproj]
1>D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.cs(94904,31): error CS1615: Argument 2 may not be passed with the 'ref' keyword [D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.csproj]
1>GENERATEFAKES : error : project compilation failed with exit code 1
1>D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.cs(94904,16): error CS1503: Argument 1: cannot convert from 'string' to 'System.Runtime.Serialization.SerializationInfo' [D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.csproj]
1>D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.cs(94904,31): error CS1615: Argument 2 may not be passed with the 'ref' keyword [D:\TestProject1\TestProject1\obj\Debug\net6.0\Fakes\sr\f.csproj]
1>GENERATEFAKES : error : project compilation failed with exit code 1
1>Done building project "TestProject1.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Changing target to Net 5.0 (after deletion of directory Fake and FakesAssemblies), add System.Runtime fake assembly, everything works.

Any idea ?

Upvotes: 6

Views: 3602

Answers (2)

Motohaya
Motohaya

Reputation: 91

I had the same problem.
I wanted to use System.Fakes.ShimDateTime. It was solved with the following fakes file:

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" Diagnostic="true">
    <Assembly Name="System.Runtime" Version="6.0.0.0" />
    <StubGeneration>
        <Clear />
        <Add FullName="System.DateTime!" />
    </StubGeneration>
    <ShimGeneration>
        <Clear />
        <Add FullName="System.DateTime!" />
    </ShimGeneration>
</Fakes>

Finally,thanks to the following answer:
https://stackoverflow.com/questions/13719617/cant-add-fakes-assembly-of-microsoft-practices-enterpriselibrary-logging-on-vs2Reference:

Upvotes: 9

wtigotw
wtigotw

Reputation: 28

In order to do this, as indicated by @Motohaya already, you seem to need to take an extra step right now. I used the answer posted already to shim File and FileInfo for my purposes of checking if the file exists and reading text from a file. Here is the code I used for System.Runtime.fakes:

<Fakes xmlns="http://schemas.microsoft.com/fakes/2011/">
  <Assembly Name="System.Runtime"/>
    <StubGeneration>
        <Clear />
        <Add FullName="System.IO.File!" />
        <Add FullName="System.IO.FileInfo!" />
        <Add FullName="System.IO.FileSystemInfo!" />
    </StubGeneration>
    <ShimGeneration>
        <Clear />
        <Add FullName="System.IO.File!" />
        <Add FullName="System.IO.FileInfo!" />
        <Add FullName="System.IO.FileSystemInfo!" />
    </ShimGeneration>
</Fakes>

This allowed me to use shims like the following:

// Arrange
var testPath = "c:\\path";

using (ShimsContext.Create())
{
    System.IO.Fakes.ShimFileInfo.AllInstances.ExistsGet =
        info => true;
    System.IO.Fakes.ShimFile.ReadAllTextString =
        path => testPath;

    // Act
    ...
}

Upvotes: 1

Related Questions