I am trying to create a couple of mock unit tests for my software test assignment and my GUI.cs file has an error that says it cannot implicitly convert type "System.Collections.IList" to "System.Collection.ArrayList"
this.accounts = database.GetAccounts();
However that file was untouched. By that I mean I didn't modify it. The only files I have modified are the unit test, the file database and the Idatabase, listed below, by changing some variable types such as renaming all my ArrayList to IList.
But my GUI has the error and I'm not able to figure out which file is the cause of it and which line of code.
Below are my files
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;
}
}
}
EDIT:
This is part of the GUI.cs file where the error is found. The error is the "this.accounts..." line.
The two "dataGridView1" lines has some System.NullReferenceException error when I hover my mouse over it on Visual Studio. Could that be related to the error I'm having, I'm not so sure.
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;
}
}
Aucun commentaire:
Enregistrer un commentaire