Unit testing 从解决方案中的其他程序访问类
我已经读过一些关于引用程序名称空间的帖子,这些程序名称空间在程序顶部有using语句。我已经添加了适当的引用,公开了类,并引用了适当的名称空间。我相信我已经安排好了。。。老实说,我觉得在这里问这样一个基本的问题有点傻,但我被难住了。我一定是错过了什么,因为它仍然不起作用,我想不出来 我试图学习如何使用Nunit运行单元测试,同时将测试与主程序分离。以下是我目前掌握的代码: Program.cs和Account.cs在第一个单独的项目中 Program.cs-Unit testing 从解决方案中的其他程序访问类,unit-testing,class,multiple-projects,Unit Testing,Class,Multiple Projects,我已经读过一些关于引用程序名称空间的帖子,这些程序名称空间在程序顶部有using语句。我已经添加了适当的引用,公开了类,并引用了适当的名称空间。我相信我已经安排好了。。。老实说,我觉得在这里问这样一个基本的问题有点傻,但我被难住了。我一定是错过了什么,因为它仍然不起作用,我想不出来 我试图学习如何使用Nunit运行单元测试,同时将测试与主程序分离。以下是我目前掌握的代码: Program.cs和Account.cs在第一个单独的项目中 Program.cs- using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NUnitTestProgramWithSeparation
{
public class MyAccountingSoftware
{
public static void Main()
{
Account DemoAccount = new Account();
DemoAccount.Deposit(1000.00F);
DemoAccount.Withdraw(500.50F);
Console.WriteLine("Our account balance is {0}", DemoAccount.Balance);
}
}
}
Account.cs-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NUnitTestProgramWithSeparation
{
public class Account
{
private float balance;
public void Deposit(float amount)
{
balance += amount;
}
public void Withdraw(float amount)
{
balance -= amount;
}
public void TransferFunds(Account destination, float amount)
{
}
public float Balance
{
get { return balance; }
}
}
}
然后,在第二个名为UnitTests的项目中,我有UnitTests.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnitTestProgramWithSeparation;
namespace UnitTests
{
[TestFixture]
public class UnitTests
{
[Test]
public void TransferFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 100.00F);
Assert.AreEqual(250.00F, destination.Balance);
Assert.AreEqual(100.00F, source.Balance);
}
[Test]
public void DepositFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Assert.AreEqual(200.00F, source.Balance);
}
}
}
在编译时,UnitTest.cs中的所有帐户引用都出现错误,表示找不到类型或命名空间名称“Account”,是否缺少using指令或程序集引用
我真的不知道我哪里出了问题。。。任何帮助都将不胜感激。我认为您只需再次编译第一个项目,然后您可以添加对第二个项目的引用,并可以使用Directive。我很抱歉,这很有效!老兄,我是不是觉得自己很笨。。。谢谢你的帮助!minhcat_vo将他/她的解决方案作为回答,这样KrazyKate就可以接受了。