![](/img/trans.png)
[英]How to resolve “The name *** does not exist in the current context” error?
[英]How to resolve error, “The name 'myClass' does not exist in the current context” for unit test?
我正在为正在构建的小型控制台应用程序编写单元测试,但是,该单元测试无法识别我从主程序引用的类。
单元测试
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
using System;
using MyApp;
namespace MyTest
{
[TestClass]
public class SampleTest
{
[TestMethod]
public void TestFunction()
{
// Arrange
string firstName = "John";
string lastName = "Smith";
string expectedOutput = "My name is John Smith";
// Act
string actualOutput = SampleClass.SampleString(name, age);
// Assert
Assert.AreEqual(expectedOutput, actualOutput);
}
}
}
主要用途
namespace MyApp
{
class Program
{
public static class SampleClass
{
public static string SampleString(string firstName, string lastName)
{
// Code here
}
}
}
}
两个项目都在同一个解决方案中,但是尽管using MyApp;
但我收到错误消息“名称'SampleClass'在当前上下文中不存在” using MyApp;
顶部的声明。
更改此行以适当地限定班级:
string actualOutput = Program.SampleClass.SampleString(name, age);
或将课程退出Program
:
namespace MyApp
{
class Program
{
} //Program ends here
public static class SampleClass
{
public static string SampleString(string firstName, string lastName)
{
//Code here
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.