You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#开发:xUnit测试项目无法识别主项目命名空间(CS0246错误)

问题:测试项目无法引用主项目命名空间(CS0246错误)

我使用Visual Studio 2022开发C#项目,最初尝试为Windows应用中的简单类编写xUnit测试,但测试类无法识别主项目命名空间的using语句。随后我改用控制台应用并放入相同的Article类,构建解决方案时仍触发CS0246错误:

The type or namespace name 'HtmlRefactoringConsole' could not be found (are you missing a using directive or an assembly reference?)
(中文译:找不到类型或命名空间名称“HtmlRefactoringConsole”(是否缺少 using 指令或程序集引用?))

控制台应用中的Article类代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HtmlRefactoringConsole
{
    public class Article
    {
        public string Title { get; }

        public string AuthorNameWithTitles { get; }

        public string InputRelativePath { get; }                                // relative path to the root directory of the Magazine 

        public string InputFileName { get; }

        private string ValidateNotNullAndNotWhiteSpace(string paramValue, string paramName)
        {
            if (paramValue == null)
            {
                throw new ArgumentNullException($"Invalid value! {paramName} is null.");
            }
            if (string.IsNullOrWhiteSpace(paramValue))
            {
                throw new ArgumentOutOfRangeException($"Invalid value! {paramName} is empty or contains only white-space character.");
            }
            return paramValue;
        }

        public Article(string title, string authorNameWithTitles, string inputRelativePath, string inputFileName)
        {
            Title = ValidateNotNullAndNotWhiteSpace(title, "Title");
            AuthorNameWithTitles = ValidateNotNullAndNotWhiteSpace(authorNameWithTitles, "AuthorNameWithTitles");
            InputRelativePath = ValidateNotNullAndNotWhiteSpace(inputRelativePath, "InputRelativePath");
            InputFileName = ValidateNotNullAndNotWhiteSpace(inputFileName, "InputFileName");
        }

        public string AuthorNameWithoutTitles()
        {
            return AuthorNameWithTitles;
        }

    }
}

xUnit测试类代码

using HtmlRefactoringConsole;

namespace HtmlRefactoringTests
{
    public class ArticleTests
    {
        [Fact]
        public void ArticleConstruction_Title_()
        {
            const string title = "Title";

            var article = new Article(title, "", "", "");

            Assert.Equal(title, article.Title);
        }
    }
}

内容的提问来源于stack exchange,提问作者ivo.tisljar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 23:54:31