Arrange, act and assert
Arrange, act and assert
A unit test checks a small public behavior under controlled conditions. Arrange prepares inputs and dependencies, act invokes the behavior, and assert compares the actual result to an independently chosen expectation. A passing test is evidence for its specific case, not proof of every requirement. The shipping fee rule used here charges 5 for orders under 50 and 0 otherwise; negative amounts are invalid.
Proposed baseline: .NET SDK 10.0.404, net10.0 / C# 14. All results are reasoned, not executed. Build and test examples require an approved isolated environment. Framework examples are separate test classes, not console entry points.
Write a package-free behavior check
This console fixture demonstrates assertion logic without installing a test framework. In real test projects, the runner collects named failures.
using System;int orderTotal = 49; // Arrangeint actual = Fee(orderTotal); // Actif (actual != 5) throw new Exception("Expected fee of 5"); // AssertConsole.WriteLine("PASS");static int Fee(int total){ if (total < 0) throw new ArgumentOutOfRangeException(nameof(total)); return total < 50 ? 5 : 0;}The expected 5 comes from the stated fee rule, not from calling Fee again. Changing Fee to always return 0 would cause this check to fail.
Expected output (reasoned, not executed):
PASSExpress the behavior with xUnit v3
This is a test class for an approved xUnit.net v3 project, not a standalone console application. Keep the test and production code in separate projects in practice. The local method makes the example self-contained.
using Xunit;public class FeeTests{ [Fact] public void Fee_BelowThreshold_ReturnsFive() { int total = 49; int actual = Fee(total); Assert.Equal(5, actual); } private static int Fee(int total) => total < 50 ? 5 : 0;}Fact identifies an example with no external data rows. The runner must discover and execute it; merely compiling the file is not a test result. The private function here covers the nonnegative input domain only.
Expected observation (reasoned, not executed):
One discovered test should pass in a correctly configured compatible xUnit v3 project.Check the invalid-input contract
Test a public behavior rather than internal implementation. This standalone check verifies the exception type and parameter name.
using System;try{ _ = Fee(-1); throw new Exception("Expected invalid-input rejection");}catch (ArgumentOutOfRangeException error) when (error.ParamName == "total"){ Console.WriteLine("Rejected total");}static int Fee(int total){ if (total < 0) throw new ArgumentOutOfRangeException(nameof(total)); return total < 50 ? 5 : 0;}An unexpected exception propagates, and no exception causes a failure. Catching any exception indiscriminately would hide defects.
Expected output (reasoned, not executed):
Rejected totalCommon mistakes and edge cases
A test that computes its expected result by calling the same implementation again is circular. Avoid dependencies on real clocks, random input, shared mutable state and real services when testing a pure rule. Use a fixed clock or a small stub for injected dependencies when necessary. Setup should establish a clean state; teardown should release resources even on failure. A test name should describe behavior and relevant conditions, not every implementation step.
Practice and recap
Write a test for an order total of exactly 50. Explain why this catches a likely incorrect implementation.
Hint: Use a fixed expected fee from the stated rule.
Learning objective: Write a test that measures a public behavior. See accompanying manual practice and knowledge check.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Arrange, act and assert — knowledge check
Apply the lesson concepts to the following scenarios.
Which step invokes the behavior?
Which expected fee should a test use for 49?
What is wrong with asserting Fee(x) equals Fee(x)?
What does a passed test establish?
Why use a stub for a clock?