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
相关产品推荐
相关产品推荐

