PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Examples/Example.DerivedSubject/DerivedSubjectSpecs.cs

https://github.com/machine/machine.specifications
C# | 70 lines | 56 code | 14 blank | 0 comment | 1 complexity | f2111778410d7c967c385cde78e6004e MD5 | raw file
Possible License(s): MIT, CC-BY-SA-3.0
  1. using System;
  2. using FluentAssertions;
  3. using Machine.Specifications;
  4. namespace Example.DerivedSubject
  5. {
  6. public class Account
  7. {
  8. private decimal _balance;
  9. public decimal Balance
  10. {
  11. get { return _balance; }
  12. set { _balance = value; }
  13. }
  14. public void Transfer(decimal amount, Account toAccount)
  15. {
  16. if (amount > _balance)
  17. {
  18. throw new Exception(String.Format("Cannot transfer ${0}. The available balance is ${1}.", amount, _balance));
  19. }
  20. _balance -= amount;
  21. toAccount.Balance += amount;
  22. }
  23. }
  24. [MySubject(typeof(Account), "Funds transfer")]
  25. public class when_transferring_between_two_accounts_with_derived_subject
  26. : AccountSpecs
  27. {
  28. Because of = () =>
  29. fromAccount.Transfer(1m, toAccount);
  30. It should_debit_the_from_account_by_the_amount_transferred = () =>
  31. fromAccount.Balance.Should().Be(0m);
  32. It should_credit_the_to_account_by_the_amount_transferred = () =>
  33. toAccount.Balance.Should().Be(2m);
  34. }
  35. [Subject(typeof(Account), "Funds transfer")]
  36. public class when_transferring_between_two_accounts_with_normal_subject
  37. : AccountSpecs
  38. {
  39. Because of = () =>
  40. fromAccount.Transfer(1m, toAccount);
  41. It should_debit_the_from_account_by_the_amount_transferred = () =>
  42. fromAccount.Balance.Should().Be(0m);
  43. It should_credit_the_to_account_by_the_amount_transferred = () =>
  44. toAccount.Balance.Should().Be(2m);
  45. }
  46. public abstract class AccountSpecs
  47. {
  48. protected static Account fromAccount;
  49. protected static Account toAccount;
  50. Establish context = () =>
  51. {
  52. fromAccount = new Account { Balance = 1m };
  53. toAccount = new Account { Balance = 1m };
  54. };
  55. }
  56. }