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.

 

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | Visual Studio | Testing
Developer technologies | Visual Studio | Other
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 34,441 Reputation points Microsoft External Staff
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.