Click here to Skip to main content
15,029,010 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new to C# and am trying to create a bank account class and than have to do the unit testing of the class. Below is my class and I am not sure why I get Cannot implicitly type bool to long error in my unit test on the

long actual = bankAccount.IsAccountNumberVerified(accountNumber);

Thank you.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project
{
    public class BankAccount
    {
        private double balance;
        private String accountHolderName;
        private long accountNumber;
        public const String WRONG_ACCOUNT_NUMBER = "Wrong account number generated";
        public const String WRONG_AMOUNT = "Cannot withdraw or deposit zero or negative amount.";
        public const String INSUFFICIENT_BALANCE = "Insufficient balance";


        public String AccountHolderName
        {
            get { return accountHolderName; }
        }

        public double Balance
        {
            get { return balance; }
        }

        public BankAccount(double initialBalance, string accountHolderName)
        {
            this.accountNumber = GetNewAccontNumber();
            this.balance = initialBalance;
            this.accountHolderName = accountHolderName;

        }



        private long GetNewAccontNumber()
        {
            Random rand = new Random();
            this.accountNumber = rand.Next(100000000, 1000000000);

            string accNumber = String.Format("32{0}", accountNumber.ToString("D6"));

            long aNum = Convert.ToInt64(accNumber);
            return aNum ;

            

        }

        public bool IsAccountNumberVerified(long accountNumber)
        {

            Boolean isVerified = true;
            String strValue = accountNumber.ToString();
            if (strValue.Length != 10)
                isVerified = false;

            if (strValue.Substring(02) != "18")
                isVerified = false;
            return isVerified;

        }


    }
}



Unit testing part:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Project;

namespace UnitTestProjectBankAccount
{
    [TestClass]
    public class UnitTestBankAccount
    {
        [TestMethod]
        public void ID_1_TestMethodIsAccountNumberVerified_BVAValidNumberMin()
        {
            //Arrange
            long accountNumber = 32186587;
            long expected = 32186587;

            //Act
            BankAccount bankAccount = new BankAccount(20.00, "Mr. Bryan Walton");
            bankAccount.IsAccountNumberVerified(accountNumber);
            long actual = bankAccount.IsAccountNumberVerified(accountNumber);


            // assert  
            Assert.AreEqual(expected, actual);

        }

    }
}
Posted
Updated 25-Aug-18 12:46pm

1 solution

IsAccountNumberVerified returns a boolean value, true or false.

A long is a 64-bit integer, not really appropriate for the return value.

Your unit test code should be:
C#
public void ID_1_TestMethodIsAccountNumberVerified_BVAValidNumberMin()
        {
            //Arrange
            long accountNumber = 32186587;
            bool expected = true;

            //Act
            BankAccount bankAccount = new BankAccount(20.00, "Mr. Bryan Walton");
            bool verified = bankAccount.IsAccountNumberVerified(accountNumber);

            // assert  
            Assert.AreEqual(verified, expected);
        }

You really can't use the account number in your unit test because it's randomly generated. Also, the random number generator is seeded with the current time when the Random object is created, so you can't even reliably get the same account number back from it.

But, this test is going to fail at a very high rate. You're testing against a very specific account number in the a range of 900,000,000 account numbers.

You wouldn't have a method called "IsAccountNumberVerified" that takes an account number as a value. The account number would be maintained and verified internally by the BackAccount class, only exposing the account number as a readonly property.
   
v3
Comments
Ron23111 25-Aug-18 19:21pm
   
Ok so is there another way to do this as I said before I am new to C# and testing. I want to my bank account class to generate random account number of 8 digits and each account number must start from 32 like 32091234, 32145498 etc...
Dave Kreskowiak 25-Aug-18 21:19pm
   
That depends on the exact test you're performing. If you're looking to see if the account number matched a specific number, you can't do that with anything close to the code you have. You have to use an interface implementation on your BankAccount class that takes an implementation of something that might be called "IAccountNumberGenerator". The concept is called "Inversion of Control".

Your IsAccountNumberVerified method would be useless in this case. If the only rule is to generate account numbers that are, say, 10 digits long and start with 32, you could easily write code that generates account numbers to that specification. There would not be a need to have a method to verify this in the BankAccount class. This is what your unit test is for.

As for the IAccountNumberGenerator interface, your BankAccount class could use a default implementation of this unless you pass in another implementation, say one made by your Unit Test.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900