Example #1
0
		public void TestProductErrorsChanged()
		{
			const decimal validValue = 5M;
			const decimal invalidValue = -5M;

			// Create a product with valid data
			var product = new Product(Guid.NewGuid(), "Test", validValue);

			// Check that no errors are reported
			Assert.IsFalse(product.HasErrors);
			Assert.IsTrue(string.IsNullOrEmpty(
				product.GetErrors(null).Cast<string>().First()));

			// Change product to invalid content and validate if
			// events are properly raised
			product.CheckIf(p => p.CostsPerItem = invalidValue)
				.Raised(nameof(Product.CostsPerItem))
				.RaisedErrorsChanged(nameof(Product.CostsPerItem));

			// Check if error status is reported
			Assert.IsTrue(product.HasErrors);
			Assert.IsFalse(string.IsNullOrEmpty(
				product.GetErrors(null).Cast<string>().First()));
			Assert.IsFalse(string.IsNullOrEmpty(
				product.GetErrors(nameof(Product.CostsPerItem)).Cast<string>().First()));
			Assert.IsTrue(string.IsNullOrEmpty(
				product.GetErrors(nameof(Product.Description)).Cast<string>().First()));

			// Call setter with identical value and check that events 
			// are not raised
			product.CheckIf(p => p.CostsPerItem = p.CostsPerItem)
				.DidNotRaise(nameof(Product.CostsPerItem))
				.DidNotRaiseErrorsChanged(nameof(Product.CostsPerItem));

			// Call setter with valid value and check that events
			// are properly raised.
			product.CheckIf(p => p.CostsPerItem = validValue)
				.Raised(nameof(Product.CostsPerItem))
				.RaisedErrorsChanged(nameof(Product.CostsPerItem));

			// Check error status has been set to OK
			Assert.IsFalse(product.HasErrors);
			Assert.IsTrue(string.IsNullOrEmpty(
				product.GetErrors(null).Cast<string>().First()));
		}