Why I can't Fake-Mock "System" in .net core 6.0(VS Enterprise 2022)

Angelina Jolie 20 Reputation points
2023-01-31T10:06:39.3233333+00:00

Why I can't Fake-Mock "System" in .net core 6.0(VS Enterprise 2022), I don't know what happened?

 

I was trying to use "using System.Fakes; " , But it alert "missing an assemly reference" after following

https://learn.microsoft.com/en-us/visualstudio/test/isolating-code-under-test-with-microsoft-fakes?view=vs-2022&tabs=csharp 

but I can see "System.fakes" file in my "Fakes" folder?

Please provide a working example in .net core for my question.

My code as below:

using System.Fakes; 

[TestClass]
public class MockDateTimeTest{
[TestMethod]
public async void TestTodayIsLastDateOfYear(){
// Arrange

// Actbool result = false;
using (ShimsContext.Create()){

ShimDateTime.NowGet = () => new DateTime(2013, 12, 31);
result = DateTimeHelper.IsTodayLastDateOfYear();
}

// Assert
Assert.IsTrue(result);
} 

}

public static class DateTimeHelper{
public static bool IsTodayLastDateOfYear()
{DateTime today = DateTime.Now;
if (today.Month == 12 && today.Day == 31)
return true;elsereturn false;
}
}

 

Let me know if you can use "using System.Fakes" in .net core successfully and please answer me as soon as possible.

Thanks in advance.

 

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,282 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,762 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
334 questions
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 28,581 Reputation points Microsoft Vendor
    2023-02-08T07:40:48.67+00:00

    Hi Angelina,

    Please kindly try to Add Fakes Assembly for System.Runtime instead of System, and let us know if it works.

    Sincerely,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful