簡體   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