我正在嘗試爲我的軟件測試任務創建幾個模擬單元測試,並且我的GUI.cs文件有一個錯誤,表示它不能隱式轉換類型 「System.Collections.IList」 到 「System.Collection.ArrayList」CS0266 - 無法將類型「System IList」隱式轉換爲「System ArrayList」 - 軟件測試
this.accounts = database.GetAccounts();
但該文件是不變。我的意思是我沒有修改它。我修改的唯一文件是單元測試,文件數據庫和Idatabase,如下所示,通過改變一些變量類型,比如將我所有的ArrayList重命名爲IList。
但我的GUI有錯誤,我無法弄清楚哪個文件是它的原因和代碼行。
下面是GUI.cs文件
的錯誤是 「this.accounts ......」 行。
當我將鼠標懸停在Visual Studio上時,兩條「dataGridView1」行出現了一些System.NullReferenceException錯誤。這可能與我遇到的錯誤有關,我不太確定。
private void InitBankRead()
{
try
{
//database = new FileDatabase();
database = IoC.GetInstance().Resolve<IDatabase>();
this.accounts = database.GetAccounts();
this.dataGridView1.DataSource = accounts;
dataGridView1.Columns["Balance"].DefaultCellStyle.Format = "C";
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
dataGridView1.Columns[column.Name].SortMode = DataGridViewColumnSortMode.Automatic;
}
}
=========================================== ==========
==================================== =================
編輯:
而且樓下是我的文件,誰想要鑽研它更深層次的休息。這是可選的,但它在那裏。我的嘗試不是用我的作品壓倒你們,而是要確保你明白我在說什麼。
IDatabase.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Banking;
namespace Banking
{
public interface IDatabase
{
//IList<Account> FindAll();
//Account FindByAccount(int accountId);
//Account FindByName(int firstName, int lastName);
//bool Save(Account target);
bool AddNewCustomer(int customerId, string firstName, string lastName, decimal openingDeposit, Account.ACCOUNTTYPE type);
bool UpdateExistingAccount(char transactionType, Account account, decimal amount);
IList GetAccounts();
}
}
FileDatabase.cs
using System;
using System.Collections;
using System.IO;
using Banking;
namespace Banking
{
public class FileDatabase : IDatabase
{
private string filename;
private StreamWriter outFile;
private StreamReader inFile;
public const string DELIMETER = ",";
public FileDatabase()
{
this.filename = @"..\..\..\Banking\Data\Database.txt";
}
public bool AddNewCustomer(int customerID, string firstName, string lastName, decimal openingDeposit, Account.ACCOUNTTYPE type)
{
int accountID = GetAccounts().Count + 1;
using (outFile = File.AppendText(filename))
{
string output = accountID + DELIMETER + customerID + DELIMETER + firstName + DELIMETER + lastName + DELIMETER + openingDeposit + DELIMETER + Convert.ToInt32(type);
outFile.WriteLine(output);
outFile.Close();
}
return true;
}
public bool UpdateExistingAccount(char transactionType, Account account, decimal amount)
{
bool success = false;
try
{
switch (transactionType)
{
case 'D':
//account.Balance += amount;
account.deposit(amount);
break;
case 'W':
//account.Balance -= amount;
account.withdrawl(amount);
break;
}
IList accounts = GetAccounts();
int accountID = account.AccountID;
// Find and replace the account
for (int i = 0; i < accounts.Count; ++i)
{
if (accountID == ((Account) accounts[i]).AccountID)
{
accounts[i] = account;
}
}
using (outFile = new StreamWriter(filename))
{
foreach (Account acct in accounts)
{
outFile.WriteLine(acct.AccountID + DELIMETER + acct.Customer.CustomerID + DELIMETER + acct.Customer.FirstName + DELIMETER + acct.Customer.LastName + DELIMETER + acct.Balance + DELIMETER + Convert.ToInt32(acct.AccountType));
}
outFile.Close();
}
success = true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
success = false;
}
return success;
}
public IList GetAccounts()
{
IList accounts = new ArrayList();
using (inFile = new StreamReader(filename))
{
int customerID;
int accountID;
string firstName;
string lastName;
decimal balance;
int accountType;
string line = string.Empty;
while ((line = inFile.ReadLine()) != null)
{
string [] data = line.Split(DELIMETER.ToCharArray()[0]);
customerID = Convert.ToInt32(data[0]);
accountID = Convert.ToInt32(data[1]);
firstName = Convert.ToString(data[2]);
lastName = Convert.ToString(data[3]);
balance = Convert.ToDecimal(data[4]);
accountType = Convert.ToInt32(data[5]);
Customer customer = new Customer(customerID, firstName, lastName);
Account account;
Account.ACCOUNTTYPE type = (Account.ACCOUNTTYPE) accountType;
if (type == Account.ACCOUNTTYPE.CHECKING)
{
account = new Checking(customer, accountID, Convert.ToDecimal(balance));
}
else
{
account = new Savings(customer, accountID, Convert.ToDecimal(balance));
}
accounts.Add(account);
}
inFile.Close();
}
return accounts;
}
}
}
UnitTest.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Banking;
using Moq;
namespace UnitTestsWithMoQ
{
[TestClass]
public partial class UnitTest1
{
public TestContext TestContext { get; set; }
public readonly IDatabase MockDatabase;
private IList accounts = new List<Account>
{
new Checking(new Customer(1, "Alex", "Parrish"), 12, 30.00M),
new Savings(new Customer(2, "Alex", "Russo"), 12, 29.00M),
new Checking(new Customer(3, "Emma", "Swan"), 12, 30.00M),
new Savings(new Customer(4, "Henry", "Mills"), 12, 30.00M)
};
Mock<IDatabase> dataMock = new Mock<IDatabase>();
private string filename;
private StreamWriter outFile;
private StreamReader inFile;
public const string DELIMETER = ",";
public UnitTest1()
{
// Return all accounts
dataMock.Setup(repository => repository.GetAccounts()).Returns(accounts);
dataMock.Setup(repository => repository.UpdateExistingAccount(It.IsAny<char>(), It.IsAny<Account>(),
It.IsAny<decimal>())).Returns((char transactionType, Account account, decimal amountDecimal) =>
{
bool success = false;
try
{
switch (transactionType)
{
case 'D':
//account.Balance += amount;
account.deposit(amountDecimal);
break;
case 'W':
//account.Balance -= amount;
account.withdrawl(amountDecimal);
break;
}
var accounts = MockDatabase.GetAccounts();
int accountID = account.AccountID;
// Find and replace the account
for (int i = 0; i < accounts.Count; ++i)
{
if (accountID == ((Account) accounts[i]).AccountID)
{
accounts[i] = account;
}
}
// Find and replace the account
for (int i = 0; i < accounts.Count; ++i)
{
MockDatabase.GetAccounts()[i] = accounts[i];
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
success = false;
}
return success;
});
dataMock.Setup(repository => repository.AddNewCustomer(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<decimal>(),
It.IsAny<Account.ACCOUNTTYPE>())).Returns((int customerID, string firstName, string lastName, decimal openingDeposit, Account.ACCOUNTTYPE type) =>
{
int accountID = MockDatabase.GetAccounts().Count + 1;
using (outFile = File.AppendText(filename))
{
string output = accountID + DELIMETER + customerID + DELIMETER + firstName + DELIMETER + lastName + DELIMETER + openingDeposit + DELIMETER + Convert.ToInt32(type);
outFile.WriteLine(output);
outFile.Close();
}
return true;
});
MockDatabase = dataMock.Object;
}
}
}
這真的很難讀懂了這一切發現的投擲異常的行。你能把它縮小到相關的部分嗎? –
當然。 GUI中引發異常。cs文件 – crhodes
它在兩條黑線之上,將其他文件分開 – crhodes