Exception not being thrown in setter

荒凉一梦 提交于 2019-12-24 22:33:59

问题


I've created a class "BankAccount.cs" and am using xunit to test it. I'm testing whether the constructor throws an ArgumentException if it's provided with an empty string as an argument (the argument is the bank account number as a string).

My test fails because no Exception is being thrown - I can't figure out why not.

It appears that the constructor isn't even being called; if I let the constructor throw an Exception (rather than implementing this in the setter of AccountNumber), none is thrown either. Also, if I use Console.WriteLine to debug manually, nothing is written to the console.

Exceptions (Debugging) are set to default settings.

using System;
using System.Collections.Generic;

namespace Banking.Models
{
    public class BankAccount : IBankAccount
    {
        /* NOTE: I've deleted non-relevant fields, properties and methods for brevity */

        private string _accountNumber;

        public string AccountNumber
        {
            get { return _accountNumber; }
            set
            {
                if(value == string.Empty)
                    throw new ArgumentException("Accountnumber must have a value");
                _accountNumber = value;
            }
        }

        public BankAccount(string account)
        {
            AccountNumber = account;
            Balance = Decimal.Zero;
            _transactions = new List<Transaction>();
        }

        public override string ToString()
        {
            return $"{AccountNumber} - {Balance}";
        }
        public override bool Equals(object obj)
        {
            BankAccount account = obj as BankAccount;
            if (account == null) return false;
            return AccountNumber == account.AccountNumber;
        }
        public override int GetHashCode()
        {
            return AccountNumber?.GetHashCode() ?? 0;
        }
    }
}

Tests:

using System;
using Xunit;
using Banking.Models;

namespace Tests.Models
{
    public class BankAccountTest : IDisposable
    {
        // private variables
        private BankAccount _account;
        private string AccountNumber { get; set; }

        // Setup
        public BankAccountTest()
        {
            AccountNumber = "123-4567890-02";
            _account = new BankAccount(AccountNumber);
        }

        [Fact]
        public void NewAccount_BalanceZero()
        {
            Assert.Equal(0, _account.Balance);
        }

        [Fact]
        public void NewAccount_CorrectAccountNumber()
        {
            Assert.Equal(AccountNumber, _account.AccountNumber);
        }

        [Fact]
        public void NewAccount_EmptyString_Fails()
        {
            Assert.Throws<ArgumentException>(
                () => new BankAccount(string.Empty));
        }

        // TearDown
        public void Dispose()
        {

        }
    }
}

来源:https://stackoverflow.com/questions/49222506/exception-not-being-thrown-in-setter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!