繁体   English   中英

使用 .NET Core Fakes 和 VS 2019 Preview 6.9.0 Preview 2.0 找不到类型或命名空间名称

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

更新 2

我在更新 1 中修复了这个问题,但下面的类似代码不起作用,并且有同样的错误。

样本来自https://docs.microsoft.com/en-us/visualstudio/test/isolating-code-under-test-with-microsoft-fakes?view=vs-2019#get-started-with-shims

目标代码

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

测试代码:

[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);
            }
        }
    }

更新 1

我按照下面的示例进行操作,

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

目标代码

 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();
        }
    }

测试代码:

[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);
            }
        }
    }

但是发生了以下错误

错误 CS0246 找不到类型或命名空间名称“ShimUserPrincipal”(您是否缺少 using 指令或程序集引用?)

任何想法?

参考文献

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

更新感谢@magicandre1981 指出这一点。

您遵循的教程适用于 .net 框架。 对于.net 核心,您必须 select System.Runtime (非系统)和 select “添加假货程序集”

在此处输入图像描述

检查页面APIsof.net哪个程序集在 .net 核心中包含一个 class。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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