问题描述
我是C#的新手,我正在尝试创建一个银行帐户类,而不是必须对该类进行单元测试。下面是我的班级,我不知道为什么我得到不能在我的单元测试中隐式输入bool到长错误
long actual = bankAccount.IsAccountNumberVerified(accountNumber) ;
谢谢。
我的尝试:
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(0, 2) != "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);
}
}
}
推荐答案
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);
}
您真的无法在单元测试中使用帐号,因为它是随机生成的。此外,随机数生成器在创建Random对象时以当前时间播种,因此您甚至无法可靠地从中获取相同的帐号。
但是,这项测试将以非常高的速度失败。您正在测试900,000,000个账号中非常具体的账号。
您将不会有一个名为IsAccountNumberVerified的方法来记账数字作为一个值。帐号将由BackAccount类在内部维护和验证,仅将帐号作为只读属性公开。
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.
这篇关于无法将类型bool转换为long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!