Пример #1
0
        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);
        }
Пример #2
0
        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 HasSubscriberOtherValue()
        {
            var mc = new MyClass("MyProperty");

            Assert.AreNotEqual(10, mc.MyProperty);

            RunTest(NotifyPropertyChanged.HasSubscriberOtherValue,
                    Assign(() => mc.MyProperty, 10),
                    SmartAssert.Raised_PropertyChanged(mc, nameof(MyClass.MyProperty), 10));
        }
Пример #4
0
        public void HasSubscriberOtherValue_NoPropertyChanged()
        {
            var exception = Assert.Catch <SmartTestException>(() =>
            {
                var mc = new MyClass(false);
                Assert.AreNotEqual(10, mc.MyProperty);

                RunTest(NotifyPropertyChanged.HasSubscriberOtherValue,
                        Assign(() => mc.MyProperty, 10),
                        SmartAssert.Raised_PropertyChanged(mc));
            }
                                                              );

            Assert.AreEqual("Event 'PropertyChanged' was expected", exception.Message);
        }
Пример #5
0
        public void HasSubscriberOtherValue_TwicePropertyChanged()
        {
            var exception = Assert.Catch <SmartTestException>(() =>
            {
                var mc = new MyClass("MyProperty", "MyProperty");
                Assert.AreNotEqual(10, mc.MyProperty);

                RunTest(NotifyPropertyChanged.HasSubscriberOtherValue,
                        Assign(() => mc.MyProperty, 10),
                        SmartAssert.Raised_PropertyChanged());
            }
                                                              );

            Assert.AreEqual("Unexpected property name 'MyProperty' when PropertyChanged event was raised", exception.Message);
        }
        public void HasSubscriberOtherValue_BadValueChanged()
        {
            var exception = Assert.Catch <SmartTestException>(() =>
            {
                var mc = new MyClass("MyProperty");
                Assert.AreNotEqual(10, mc.MyProperty);

                RunTest(NotifyPropertyChanged.HasSubscriberOtherValue,
                        Assign(() => mc.MyProperty, 10),
                        SmartAssert.Raised_PropertyChanged(mc, "MyProperty", 11));
            }
                                                              );

            Assert.AreEqual("Change is wrong. Expected 11, but was 10", exception.Message);
        }
Пример #7
0
        public void SetAddress_HappyPath()
        {
            RunTest(ValidString.HasContent,
                    // Act
                    // To Test Assignment, use Assign
                    Assign(() => _Customer.Address, "Quebec"),

                    // Assert (and implicit Assume)
                    // Before Act: Register to PropertyChangedEvent
                    // After Act: Assert PropertyChanged raised when assignment is run, with "Address" as PropertyName
                    SmartAssert.Raised_PropertyChanged(),
                    // Before Act: Assume _Customer.Address != "Quebec" before assignment, keep track of all other public properties
                    // AfterAct: _Customer.Address == "Quebec", all other public properties did not change
                    SmartAssert.NotChangedExceptAct()
                    );
        }