public void Deposit_HappyPath() { var lowerNow = DateTime.Now; // We use a lambda expression to show equivalence class and to generate a random value within this equivalence class RunTest(Case(( double amount ) => amount.Above(0), out var value), // Act () => _Account.Deposit(value), // Assert (and implicit Assume) // Before Act: Keep tracks of all public properties // After Act: Assert all public properties have the same values, except Balance SmartAssert.NotChangedExcept(nameof(Account.Balance)), // Before Act: Register to PropertyChangedEvent // After Act: Assert PropertyChanged is raised for Balance SmartAssert.Raised_PropertyChanged(_Account, nameof(Account.Balance)), // Before Act: Compute _Account.Balance + value // After Act: Assert _Account.Balance is previous computed value SmartAssert.Change(() => _Account.Balance + value), // Before Act: Compute _Account.Transactions.Count + 1 // After Act: Assert _Account.Transactions.Count is previous computed value SmartAssert.Change(() => _Account.Transactions.Count + 1) ); // Assert the added transaction reflects the Deposit var transaction = _Account.Transactions.Last(); Assert.AreEqual(_Account, transaction.Account); Assert.AreEqual(value, transaction.Amount); Assert.IsTrue(lowerNow <= transaction.Date && transaction.Date <= DateTime.Now); Assert.AreEqual(TransactionKind.Deposit, transaction.Kind); Assert.IsNull(transaction.SecondAccount); }
public void CreateAccount() { var previous = _Customer.CreateAccount(); var account = RunTest( // We always can call CreateAccount AnyValue.IsValid, // Act () => _Customer.CreateAccount(), // Assert (and implicit Assume) // Before act: Register to PropertyChangedEvent // After Act: Assert not PropertyChanged is raised SmartAssert.NotRaised_PropertyChanged(), // Before Act: Keep track of all properties and fields // After Act: Assert no property nor field changed => the _Customer is not changed at all SmartAssert.NotChanged(NotChangedKind.All), // Before Act: Compute the expression _Customer.Account.Count + 1 and save its result // After Act: Assert current _Customer.Accounts.Count is the saved value (thus, that Count is one more than before) SmartAssert.Change(() => _Customer.Accounts.Count + 1) ); // Assert Assert.AreSame(account, _Customer.Accounts.Last()); Assert.AreEqual(previous.Id + 1, account.Id); Assert.AreSame(_Customer, account.Customer); Assert.AreEqual(0, account.Balance); Assert.IsEmpty(account.Transactions); }
public void Transfer_HappyPath() { var lowerNow = DateTime.Now; _Account.Deposit(1000); var account2 = _Customer.CreateAccount(); // We use a lambda expression to show equivalence class and to generate a random value within this equivalence class var success = RunTest(Case(( double amount ) => amount.Range(0, false, 1000, true), out var value) & Case("toAccount", ValidValue.IsValid), // Act () => _Account.Transfer(value, account2), // Assert (and implicit Assume) // Before Act: Keep tracks of all public properties // After Act: Assert all public properties have the same values, except Balance SmartAssert.NotChangedExcept(nameof(Account.Balance)), // Before Act: Keep tracks of all public properties of account2 // After Act: Assert all public properties of account2 have the same values, except Balance SmartAssert.NotChangedExcept(account2, nameof(Account.Balance)), // Before Act: Register to PropertyChangedEvent // After Act: Assert PropertyChanged is raised for Balance SmartAssert.Raised_PropertyChanged(_Account, nameof(Account.Balance)), // Before Act: Register to PropertyChangedEvent // After Act: Assert PropertyChanged is raised for Balance SmartAssert.Raised_PropertyChanged(account2, nameof(Account.Balance)), // Before Act: Compute _Account.Balance - value // After Act: Assert _Account.Balance is previous computed value SmartAssert.Change(() => _Account.Balance - value), // Before Act: Compute account2.Balance - value // After Act: Assert account2.Balance is previous computed value SmartAssert.Change(() => account2.Balance + value), // Before Act: Compute _Account.Transactions.Count + 1 // After Act: Assert _Account.Transactions.Count is previous computed value SmartAssert.Change(() => _Account.Transactions.Count + 1), // Before Act: Compute account2.Transactions.Count + 1 // After Act: Assert account2.Transactions.Count is previous computed value SmartAssert.Change(() => account2.Transactions.Count + 1) ); Assert.IsTrue(success); // Assert the added transaction reflects the Transfer var transaction = _Account.Transactions.Last(); Assert.AreEqual(_Account, transaction.Account); Assert.AreEqual(-value, transaction.Amount); Assert.IsTrue(lowerNow <= transaction.Date && transaction.Date <= DateTime.Now); Assert.AreEqual(TransactionKind.Transfer, transaction.Kind); Assert.AreEqual(account2, transaction.SecondAccount); // Assert the added transaction reflects the Transfer var transaction2 = account2.Transactions.Last(); Assert.AreEqual(account2, transaction2.Account); Assert.AreEqual(value, transaction2.Amount); Assert.IsTrue(lowerNow <= transaction2.Date && transaction2.Date <= DateTime.Now); Assert.AreEqual(TransactionKind.Transfer, transaction2.Kind); Assert.AreEqual(_Account, transaction2.SecondAccount); }
public void PropertyChange() { _Mc.ChangeProperty = true; RunTest(AnyValue.IsValid, () => _Mc.Method(), SmartAssert.Change(() => _Mc.MyProperty + 1)); }
public void FieldChange() { _Mc.ChangeField = true; RunTest(AnyValue.IsValid, () => _Mc.Method(), SmartAssert.Change(() => _Mc.MyField + 4)); }
public void IndexerChange() { _Mc.ChangeIndexer = true; RunTest(AnyValue.IsValid, () => _Mc.Method(), SmartAssert.Change(() => _Mc[0] + 2)); }
public void IndirectChange() { _Mc.ChangeIndirect = true; RunTest(AnyValue.IsValid, () => _Mc.Method(), SmartAssert.Change(() => _Mc.Items.Count + 1)); }
public void PropertyChange() { var mc = new MyClass(true, false, false, false); RunTest(AnyValue.IsValid, () => mc.Method(), SmartAssert.Change(() => mc.MyProperty + 1)); }
public void FieldChange() { var mc = new MyClass(false, false, false, true); RunTest(AnyValue.IsValid, () => mc.Method(), SmartAssert.Change(() => mc.MyField + 4)); }
public void IndexerChange() { var mc = new MyClass(false, true, false, false); RunTest(AnyValue.IsValid, () => mc.Method(), SmartAssert.Change(() => mc[0] + 2)); }
public void IndirectChange() { var mc = new MyClass(false, false, true, false); RunTest(AnyValue.IsValid, () => mc.Method(), SmartAssert.Change(() => mc.Items.Count + 1)); }
public void PropertyChangeError() { var exception = Assert.Catch <SmartTestException>(() => { _Mc.ChangeProperty = true; RunTest(AnyValue.IsValid, () => _Mc.Method(), SmartAssert.Change(() => _Mc.MyProperty + 2)); }); Assert.AreEqual("Change is wrong. Expected 2, but was 1", exception.Message); }
public void IndexerChangeError() { var exception = Assert.Catch <SmartTestException>(() => { _Mc.ChangeIndexer = true; RunTest(AnyValue.IsValid, () => _Mc.Method(), SmartAssert.Change(() => _Mc[0] + 3)); }); Assert.AreEqual("Change is wrong. Expected 3, but was 2", exception.Message); }
public void FieldChangeError() { var exception = Assert.Catch <SmartTestException>(() => { var mc = new MyClass(false, false, false, true); RunTest(AnyValue.IsValid, () => mc.Method(), SmartAssert.Change(() => mc.MyField + 2)); }); Assert.AreEqual("Change is wrong. Expected 2, but was 4", exception.Message); }
public void IndirectChangeError() { var exception = Assert.Catch <SmartTestException>(() => { var mc = new MyClass(false, false, true, false); RunTest(AnyValue.IsValid, () => mc.Method(), SmartAssert.Change(() => mc.Items.Count + 2)); }); Assert.AreEqual("Change is wrong. Expected 3, but was 2", exception.Message); }
public void CloseAccount_HappyPath() { var account = _Customer.CreateAccount(); var result = RunTest(CollectionItem.IsInCollection, // Act () => _Customer.CloseAccount(account), // Assert (and implicit Assume) // Before Act: Keep track of all properties and fields // After Act: Assert no property nor field changed => the _Customer is not changed at all SmartAssert.NotChanged(NotChangedKind.All), // Before Act: Register to PropertyChangedEvent // After Act: Assert not PropertyChanged is raised SmartAssert.NotRaised_PropertyChanged(), // Before Act: Compute the expression _Customer.Account.Count - 1 and save its result // After Act: Assert current _Customer.Accounts.Count is the saved value (thus, that Count is one less than before) SmartAssert.Change(() => _Customer.Accounts.Count - 1) ); // Assert Assert.IsTrue(result); }