Skip to content

Team 1 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions TDDLesson.BankAccount.Tests/BankAccountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,117 @@

using FluentAssertions;

/*
* - Добавление на счет денеждых средств
* - Снятие средств со счета
*/


[TestClass]
public sealed class BankAccountTests
{
[TestMethod]
public void ShouldBe20OnAccount_When20Added()
{
// Arrange
var balance = 20;
var account = new BankAccount(0);

// Act
var newAccount = account.Add(balance);

// Assert
newAccount.Balance.Should().Be(balance);
}

[TestMethod]
public void ShouldBe50OnAccount_When20AddedAnd30Was()
{
// Arrange
var balanceBefore = 30;
var balanceAdded = 20;
var account = new BankAccount(balanceBefore);

// Act
var newAccount = account.Add(balanceAdded);

// Assert
newAccount.Balance.Should().Be(50);
}

[TestMethod]
public void ShouldThrow_WhenNegativeValueAdded()
{
// Arrange
var balanceBefore = 30;
var balanceAdded = -20;
var account = new BankAccount(balanceBefore);

// Act
var newAccount = () => account.Add(balanceAdded);

// Assert
newAccount.Should().ThrowExactly<ArgumentException>();
}

[TestMethod]
public void ShouldBe20OnAccount_When10Withdrawn()
{
// Arrange
var balanceBefore = 30;
var balance = 10;
var account = new BankAccount(0, balanceBefore);

// Act
var newAccount = account.Withdraw(balance);

// Assert
newAccount.Balance.Should().Be(20);
}

[TestMethod]
public void ShouldThrow_WhenNegativeValueWithdrawn()
{
// Arrange
var balanceBefore = 30;
var balanceWithdrawn = -20;
var account = new BankAccount(0, balanceBefore);

// Act
var newAccount = () => account.Withdraw(balanceWithdrawn);

// Assert
newAccount.Should().ThrowExactly<ArgumentException>();
}

[TestMethod]
public void ShouldThrow_WhenWithdrawnMoreThanOnAccountIncludingOverdraft()
{
// Arrange
var balanceOverdraft = 10;
var balanceBefore = 30;
var balanceWithdrawn = 50;
var account = new BankAccount(0, balanceBefore);

// Act
var newAccount = () => account.Withdraw(balanceWithdrawn);

// Assert
newAccount.Should().ThrowExactly<InvalidOperationException>();
}

[TestMethod]
public void ShouldBeMinus50OnAccount_WhenWithdrawn50AndZeroOnAccount()
{
// Arrange
var balanceBefore = 10;
var balanceWithdrawn = 50;
var account = new BankAccount(0, balanceBefore);

// Act
var newAccount = account.Withdraw(balanceWithdrawn);

// Assert
newAccount.Balance.Should().Be(-40);
}
}
18 changes: 12 additions & 6 deletions TDDLesson.BankAccount/BankAccount.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
namespace TDDLesson.BankAccount;

public sealed class BankAccount
public record BankAccount(int Overdraft, int Balance = 0)
{
public int Balance { get; private set; }
public int Balance { get; private set; } = Balance;

public void Add(int money)
public BankAccount Add(int money)
{
if (money < 0) throw new InvalidOperationException();

Balance = money;
if (money < 0) throw new ArgumentException();
return new BankAccount(Balance + money);
}

public BankAccount Withdraw(int money)
{
if (money < 0) throw new ArgumentException();
if (Balance + Overdraft - money < 0) throw new InvalidOperationException();
return new BankAccount(Balance + Overdraft - money, Overdraft);
}
}