Tôi đang cố gắng kiểm tra tệp (Account.cs) bằng cách sử dụng tệp thử nghiệm (AccountTest.cs). Tôi chạy OSX 10.6 với Mono Framework (và nunit-console).Phạm vi mã sử dụng thử nghiệm đơn điệu và đơn vị
Dưới đây là Account.cs
namespace bank
{
using System;
public class InsufficientFundsException : ApplicationException
{
}
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)
{
destination.Deposit(amount);
Withdraw(amount);
}
public float Balance
{
get { return balance;}
}
private float minimumBalance = 10.00F;
public float MinimumBalance
{
get{ return minimumBalance;}
}
}
}
Và đây là AccountTest.cs:
namespace bank
{
using NUnit.Framework;
[TestFixture]
public class AccountTest
{
[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]
[ExpectedException(typeof(InsufficientFundsException))]
public void TransferWithInsufficientFunds()
{
Account source = new Account();
source.Deposit(200.00F);
Account destination = new Account();
destination.Deposit(150.00F);
source.TransferFunds(destination, 300.00F);
}
}
}
tôi biên soạn hai tập tin này bằng cách:
mcs -t:library Account.cs
mcs -t:library -r:nunit.framework,Account.dll AccountTest.cs
Và nhận tài khoản .dll và AccountTest.dll tương ứng.
Để chạy thử nghiệm tôi sử dụng:
nunit-console AccountTest.dll
và nó chạy như bình thường, đem lại cho tôi những thất bại phù hợp và vượt qua.
Tuy nhiên, bây giờ tôi muốn sử dụng khả năng bao phủ mã của mono để đánh lừa các thử nghiệm này. Tôi đang đọc hướng dẫn http://mono-project.com/Code_Coverage để chạy các công cụ phủ sóng. Và để sử dụng nó, tôi sẽ cần phải biên dịch vào tập tin * .exe chứ không phải là tập tin * .dll.
Nếu ai đó có thể giúp tôi với lớp chính của tệp AccountTest.cs, tôi sẽ có thể biên dịch nó trong exe và từ đó sử dụng công cụ bảo hiểm.
Cảm ơn trước một tấn.