简体   繁体   中英

The type or namespace name could not be found with .NET Core Fakes and VS 2019 Preview 6.9.0 Preview 2.0

Update 2

I fixed the problem in Update 1, but the similar code below doesn't work, and has the same error.

The sample is from https://docs.microsoft.com/en-us/visualstudio/test/isolating-code-under-test-with-microsoft-fakes?view=vs-2019#get-started-with-shims

Target code

  public class DateShimDemo
    {
        public int GetTheCurrentYear()
        {
            return DateTime.Now.Year;
        }
    }

Test code:

[TestClass]
    public class DateShimDemoTest
    {
        [TestMethod]
        public void TestCurrentYear()
        {
            int fixedYear = 2000;

            // Shims can be used only in a ShimsContext:
            using (ShimsContext.Create())
            {
                // Arrange:
                // Shim DateTime.Now to return a fixed date:
                System.Fakes.ShimDateTime.NowGet  
   //The type or namespace name 'Fakes' does not exist in the namespace 'System'
                   = () =>   
                    { return new DateTime(fixedYear, 1, 1); };

                // Instantiate the component under test:
                var componentUnderTest = new DateShimDemo();

                // Act:
                int year = componentUnderTest.GetTheCurrentYear();

                // Assert:
                // This will always be true if the component is working:
                Assert.AreEqual(fixedYear, year);
            }
        }
    }

Update 1

I followed the sample below,

https://medium.com/@ckellywilson/visual-studio-2019-and-net-core-fakes-fea47caccdc8

Target code

 public class SampleUserSecurity
    {
        public SampleUserSecurity()
        {
        }

        public SampleUserSecurity(string domain, string username, string password)
        {
            this.UserPrincipal = new UserPrincipal(new PrincipalContext(ContextType.Domain, domain, username, password));
        }

        public UserPrincipal UserPrincipal { get; set; }

        public bool IsAccountLocked()
        {
            return this.UserPrincipal.IsAccountLockedOut();
        }
    }

Test code:

[TestClass]
    public class SampleUserSecurityTests
    {
        [TestMethod]
        public void Test1()
        {
            using (ShimsContext.Create())
            {
                var target = new SampleUserSecurity();

                var shimUserPrincipal = new ShimUserPrincipal();  //error here
                var shimAuthtenticatedPrincipal = new ShimAuthenticablePrincipal(shimUserPrincipal) { IsAccountLockedOut = () => false };
                target.UserPrincipal = shimUserPrincipal;

                bool isLocked = false;
                bool testIsLocked = target.IsAccountLocked();

                Assert.Equal(isLocked, testIsLocked);
            }
        }
    }

But an error below occurred

Error CS0246 The type or namespace name 'ShimUserPrincipal' could not be found (are you missing a using directive or an assembly reference?)

Any idea?

Refs

https://docs.microsoft.com/en-us/visualstudio/test/isolating-code-under-test-with-microsoft-fakes?view=vs-2019

Update Thanks @magicandre1981 for pointing it out.

The tutorials you follow are for .net framework. For .net core you have to select System.Runtime (not System) and select "Add Fakes Assembly"

在此处输入图像描述

Check the page APIsof.net which assembly includes a class in .net core.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM