-
Notifications
You must be signed in to change notification settings - Fork 0
Steps
If you want to describe steps of the fixture, you can inherit FixtureSteppable and define them using the method that is defined in it.
[Specification]
class CustomerSpec : FixtureSteppable {}
The available steps are as follows.
Describes the expectation of the behavior.
The assertion is specified with the expression of a function that returns boolean value or an action that throws an exception if the assertion is failed. However, an assertion method is not defined in Carna, you should implement them or use an assertion framework such as FluentAssertions.
For example:
Expect("the result should be 5", () => ActualResult == 5);
Expect("the reuslt should be 5", () => ActualResult.Should().Be(5)); // use FluentAssertions
Note 1: When the expression of the assertion is the BinaryExpression, you should specify that the left operand is an actual value and the right operand an expected value. If the assertion is failed, the failure description is the following format: "expected: {a value of the right operand} but was: {a value of the left operand}.
Note 2: When the expression of the assertion is the MethodCallExpression that is object.Equals(object), you should specify that the caller object is an actual value and the parameter object is an expected value. If the assertion is failed, the failure description is the following format: "expected: {the parameter object} but was: {the caller object}.
Note 3: When the expression of the assertion is the MethodCallExpression that is Equals(object1, object2), you should specify that the first parameter object is an actual value and the second parameter object is an expected value. If the assertion is failed, the failure description is the following format: "expected: {the second parameter object} but was: {the first parameter object}.
Note 4: If you specify only a description, the result will be pending.
Describes the state before beginning the behavior.
The state before beginning the behavior is specified with an Action delegate.
For example:
Given("a customer whose name is 'John'", () => Customer = new Customer("John"));
Given("a user that is logged in", async () => await User.Login());
Note 1: Given step must be before When or Then step. If not, InvalidFixtureStepException is thrown and the step is failed.
Note 2: If you specify only a description, the result will be pending.
Describes the behavior.
The behavior is specified with an Action delegate. The time-out value can also be specified.
For example:
When("user adds the product to the cart", () => Cart.AddProduct(Product));
When("user checks out", async () => await Cart.Checkout());
When("user adds the product to the cart", 300, () => Cart.AddProduct(Product));
When("user checks out", TimeSpan.FromMilliseconds(500), async () => await Cart.Checkout());
Note: If you specify only a description, the result will be pending.
Describes the expectation of the behavior.
The assertion is specified with the expression of a function that returns boolean value or an action that throws an exception if the assertion is failed. However, an assertion method is not defined in Carna, you should implement them or use an assertion framework such as FluentAssertions.
If you want to assert the exception that is thrown in the When step, you can use the assertion that has it as a parameter or use the assertion with an exception type parameter.
For example:
Then("the user should be an administrator", () => User.IsAdministrator);
Then("the total price is 1500", () => Cart.TotalPrice.Should().Be(1500)); // use FluentAssertions
Then("InvalidOperationException should be thrown", exc => exc.GetType() == typeof(InvalidOperationException));
Then("the inner exception of the exception should be null", exc => exc.InnerException.Should().BeNull()); // use FluentAssertions
Then<InvalidOperationException>("InvalidOperationException should be thrown");
Then<InvalidOperationException>("the inner exception of the exception should be null", exc => exc.InnerException.Should().BeNull()); // use FluentAssertions
Note 1: Then step must be after When step. If not, InvalidFixtureStepException is thrown and the step is failed.
Note 2: When the expression of the assertion is the BinaryExpression, you should specify that the left operand is an actual value and the right operand an expected value. If the assertion is failed, the failure description is the following format: "expected: {a value of the right operand} but was: {a value of the left operand}.
Note 3: When the expression of the assertion is the MethodCallExpression that is object.Equals(object), you should specify that the caller object is an actual value and the parameter object is an expected value. If the assertion is failed, the failure description is the following format: "expected: {the parameter object} but was: {the caller object}.
Note 4: When the expression of the assertion is the MethodCallExpression that is Equals(object1, object2), you should specify that the first parameter object is an actual value and the second parameter object is an expected value. If the assertion is failed, the failure description is the following format: "expected: {the second parameter object} but was: {the first parameter object}.
Note 5: If you specify only a description, the result will be pending. But when an exception type is specified, an assertion that asserts whether the thrown exception is equal to the specified exception type is executed.
Describes the note that you want to output during the step running.
For example:
Note($"the total price of the cart is {Cart.TotalPrice}");