无法将类型bool转换为long [英] Cannot convert type bool to long

查看:207
本文介绍了无法将类型bool转换为long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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);

        }

    }
}

推荐答案

IsAccountNumberVerified返回一个布尔值,true或false。



long是64位整数,不适合返回值。



您的单元测试代码应为:

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:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆