Пример #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 Property1Changed()
        {
            var mc = new MyClass(true, false, false, false);

            RunTest(AnyValue.IsValid,
                    () => mc.Method(),
                    SmartAssert.NotChangedExcept(mc, NotChangedKind.AllProperties, "MyProperty1"));
        }
        public void AllPropertiesChanged_Except1234()
        {
            var mc = new MyClass(true, true, true, true);

            RunTest(AnyValue.IsValid,
                    () => mc.Method(),
                    SmartAssert.NotChangedExcept(mc, NotChangedKind.AllProperties, "MyProperty1", "MyProperty2", "MyProperty3", "MyProperty4"));
        }
Пример #5
0
        public void NotChangedExcept()
        {
            var mc = new MyClass();

            RunTest(AnyValue.IsValid,
                    Assign(() => mc.MyProperty1, 1),
                    SmartAssert.NotChangedExcept(),
                    SmartAssert.ChangedTo());
        }
        public void BadProperty()
        {
            var exception = Assert.Catch <InconclusiveException>(() =>
            {
                var mc = new MyClass(false, false, false, false);

                RunTest(AnyValue.IsValid,
                        () => mc.Method(),
                        SmartAssert.NotChangedExcept(mc, NotChangedKind.AllProperties, "BadProperty"));
            });

            Assert.AreEqual("BAD TEST: 'BadProperty' is not a property nor a field of type 'NotChangedTestsExcept+MyClass'", exception.Message);
        }
        public void AllPropertiesChanged_ImplicitPublicImplicitInstance()
        {
            var exception = Assert.Catch <SmartTestException>(() =>
            {
                var mc = new MyClass(true, true, true, true);

                RunTest(AnyValue.IsValid,
                        () => mc.Method(),
                        SmartAssert.NotChangedExcept("MyProperty1"));
            });

            Assert.AreEqual("Property 'NotChangedTestsExcept+MyClass.MyProperty2' has changed", exception.Message);
        }
        public void AllPropertiesChanged_ImplicitPublicWithNonPublic()
        {
            var exception = Assert.Catch <InconclusiveException>(() =>
            {
                var mc = new MyClass(true, true, true, true);

                RunTest(AnyValue.IsValid,
                        () => mc.Method(),
                        SmartAssert.NotChangedExcept(mc, "MyProperty1", "MyProperty3"));
            });

            Assert.AreEqual("BAD TEST: 'MyProperty3' is not a property nor a field of type 'NotChangedTestsExcept+MyClass'", exception.Message);
        }
        public void AllPropertiesChanged_Except123()
        {
            var exception = Assert.Catch <SmartTestException>(() =>
            {
                var mc = new MyClass(true, true, true, true);

                RunTest(AnyValue.IsValid,
                        () => mc.Method(),
                        SmartAssert.NotChangedExcept(mc, NotChangedKind.AllProperties, "MyProperty1", "MyProperty2", "MyProperty3"));
            });

            StringAssert.Contains("Property 'NotChangedTestsExcept+MyClass.MyProperty4' has changed", exception.Message);
        }
        public void Property4Changed()
        {
            var exception = Assert.Catch <SmartTestException>(() =>
            {
                var mc = new MyClass(false, false, false, true);

                RunTest(AnyValue.IsValid,
                        () => mc.Method(),
                        SmartAssert.NotChangedExcept(mc, NotChangedKind.AllProperties, "MyProperty3"));
            });

            Assert.AreEqual("Property 'NotChangedTestsExcept+MyClass.MyProperty4' has changed", exception.Message);
        }
Пример #11
0
        public void NotChangedExcept_BadExcept()
        {
            var exception = Assert.Catch <SmartTestException>(() =>
            {
                var mc = new MyClass();

                RunTest(AnyValue.IsValid,
                        Assign(() => mc.MyProperty2, 1),
                        SmartAssert.NotChangedExcept(),
                        SmartAssert.ChangedTo());
            });

            StringAssert.Contains("Property 'NotChangedTestsExceptImplicit+MyClass.MyProperty3' has changed", exception.Message);
        }