我正在尝试更多地使用Nunit,而且我是一个完整的菜鸟。我写了一些测试方法,似乎认为有空引用。
测试类
using LibraryBL.Loan;
using LibraryBLMock.Mock;
using LibraryBLModel.Model;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibraryBLTests
{
[TestFixture]
public class LoanManagementTests
{
[Test]
public void CheckReservedFalse() // Passes if reserved = false
{
LoanManagement LoanManagement = new LoanManagement();
MockPublications MockPublications = new LibraryBLMock.Mock.MockPublications();
List<Publication> l = new List<Publication>();
l = MockPublications.MockListPublications();
}
[Test]
public void InitCheckOutTest() // Passes if reserved = false
{
LoanManagement LoanManagement = new LoanManagement();
LoanManagement.InitCheckOut("0-330-28498-4");
}
}
}
目前我没有使用资产方法测试任何值,但对于CheckReservedFalse,我想测试我的方法返回false。
我想测试的类方法
public class LoanManagement
{
#region ReservePublication
public void InitCheckOut(string ISBN)
{
if (!CheckReserved(ISBN))
{
}
}
public bool CheckReserved(string ISBN)
{
List<Publication> p = GetPublicationsByISBN("0-330-28498-4");
if (p.Count == 0)
{
return false;
}
else
{
return true;
}
}
public List<Publication> GetPublicationsByISBN(string ISBN)
{
LibraryBLMock.Mock.MockPublications m = new LibraryBLMock.Mock.MockPublications(); // Use MOCK instead of data layer
return m.MockListPublications()
.Where(i => i.ISBN == ISBN && i.reserved == false).ToList();
}
public void SetPublication(string Status)
{
switch(Status)
{
case "CheckOut":
// DB method to set book as reserved
break;
}
}
#endregion
public void CheckOutPublication()
{
string s = "";
if(CheckReserved("0-330-28498-4"))
{
s = "This publication is not available at this current time";
}
else
{
//s = "There are" + p.Count + " of" + p.FirstOrDefault().Title + " available for loan";
}
}
所以我想知道如何测试我的对象返回的值。为什么我在我的代码中编译时会得到一个空引用,我正在实例化那些类。
道歉,如果我的问题有点愚蠢。