Sub topic 6
Asserting the expected actions
This chapter assumes you know what an Agentforce topic and action are, that you can read a YAML test spec, and that you have already seen the parts of a test case (utterance, expected topic, expected actions, expected outcome). Here you will learn how to write the expected actions assertion well, and how to avoid the mistakes that make it fail for the wrong reasons.
What an action assertion checks
An action is a unit of work the agent can run, such as a Flow, an Apex method, or a prompt template. After the agent picks a topic, it decides which of that topic's actions to call. The expected actions field of a test case states which actions the agent should call for the utterance.
This assertion is the most concrete check in a test case. The topic says where the agent routed the request. The outcome is a judgment about the reply text. The action list says whether the agent did the work. An agent can choose the right topic and still never call the action that looks up the order. It can then answer from general knowledge with a confident but unfounded reply.
The test compares the actions the agent called with the list you wrote. You write the list as developer names, not as the labels you see in Agent Builder.
testCases:
- utterance: Where is my order 1001?
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
expectedOutcome: The agent tells the customer the current status of the order.Where the action fits in a run
The diagram shows why the assertions are separate. If routing fails, the actions in that run are usually wrong too, so read the topic result first.
Use developer names
The action names in your spec must match the developer names (API names) of the actions in your agent. This is the most common source of failures that are not the agent's fault. A test that says Get Order Status or getOrderStatus will fail even if the agent did exactly the right thing.
To find the correct names:
- Open the agent in Agent Builder and open the topic.
- Open each action and read its API name, not its label.
- Copy the name into the spec exactly, including capitalization and underscores.
If you rename an action later, update every test that references it. A rename is a common reason a whole group of tests turns red at once.
Three patterns to write
One action
Most single-purpose requests map to one action. List the one name.
expectedActions:
- Get_Order_StatusSeveral actions
Some requests need more than one action. A customer who asks to cancel an order may need the agent to look the order up and then cancel it. List every action you expect.
- utterance: Cancel order 1001, I ordered it by mistake.
expectedTopic: Order_Changes
expectedActions:
- Get_Order_Status
- Cancel_Order
expectedOutcome: The agent confirms that order 1001 has been cancelled.When you write a multi-action case, run it and look at what the agent actually did before you decide the list is final. The order of calls and how your release treats extra or missing actions are details to confirm against your own results, not to assume.
No action
Some requests should end without any action. Examples are greetings, requests that are out of scope, and questions the agent answers from its instructions. Write an empty list.
- utterance: What is the best pizza recipe?
expectedTopic: Off_Topic
expectedActions: []
expectedOutcome: The agent politely declines and explains what it can help with.An empty list is a real assertion. It says the agent must not call anything. This is useful for out-of-scope and risky requests, where an unwanted call to a refund or update action would be a bug.
Actions that need inputs
Many actions need inputs, such as an order number. The assertion checks which action ran, not what you typed into it. If the utterance does not contain the input, the agent may ask a follow-up question instead of calling the action. The test then sees no action call and fails.
There are two ways to handle this:
- Put the needed value in the utterance, as in
Where is my order 1001?. - Test the clarifying question on purpose. Use an utterance with no order number, expect an empty action list, and write an outcome that says the agent asks for the order number.
Both are valid. Pick one per case, and say in the description which you meant.
- utterance: Where is my order?
expectedTopic: Order_Status
expectedActions: []
expectedOutcome: The agent asks the customer for the order number before looking anything up.Reading an action failure
When an action assertion fails, compare the expected list with the actual list in the result. Each shape of mismatch points to a different cause.
| What you see | Likely cause | What to check |
|---|---|---|
| Expected action missing, actual list empty | The agent did not call it, or needed an input it did not have | Topic instructions, the action description, whether the utterance contains the required input |
| Actual list has a different action | Two actions have overlapping descriptions | Rewrite the action descriptions so each states when to use it |
| Names look identical but fail | Label used instead of developer name, or a typo | The spec against the API name in Agent Builder |
| Expected empty, actual has an action | The agent acted when it should not have | Topic scope and instructions that limit the action |
| Topic also failed | Routing error upstream | Fix the topic first, then rerun |
Decide whether the failure belongs to the agent or the test. If the agent behaved sensibly and your list was wrong, fix the test. If the agent skipped a needed action, fix the agent: usually the action description or the topic instructions, which is where the model reads when to call something.
Keep assertions about behavior, not wording
An action assertion is stable because it does not depend on how the model phrases a reply. Lean on it. If you can state a requirement as "the agent must call X" or "the agent must not call anything", assert it here rather than hoping the outcome text captures it. The outcome check is looser and is better for what the reply says.
Avoid listing actions you hope the agent calls but that the requirement does not demand. Each extra name is another way for the test to fail on a harmless variation.
Running the check from the CLI
After you edit the spec, push the change and run the suite. Creating the test from the spec and running it use the commands from the CLI chapter.
sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --target-org my-org
sf agent test run --api-name Order_Support_Agent_Tests --wait 10 --result-format human --target-org my-orgIn the results, find the failing case and compare its expected and actual action lists, as in the table above.
Summary of rules
- Use the action's developer name exactly as it appears in the agent.
- List every action the requirement demands, and no more.
- Use
[]when the agent must not call any action. - Include required inputs in the utterance, or test the follow-up question deliberately.
- On failure, check the topic first, then compare the expected and actual lists.
Exercise
You write a test with the utterance "Please refund order 1001" and expectedActions of [Issue_Refund]. The test fails. The result shows the topic was correct and the actual action list contains Get_Order_Status but not Issue_Refund. Name two possible causes and say what you would change for each.
Show answer
First, the refund action's description or the topic instructions may not tell the agent clearly when to call it, or may require the agent to look up the order first. Fix the agent by clarifying the action description or instructions. If the lookup step is the intended behavior, change the test to expect both Get_Order_Status and Issue_Refund. Second, the name in the spec may not match the action's developer name (for example, a renamed action). Check the API name in Agent Builder and correct the spec. Either way, the topic passed, so the problem is in action selection or in the test, not routing.
Exercise
Write the test case for the utterance "Hi there" in the OrderSupportAgent, assuming the agent should greet the customer and call no actions. Which topic would you expect, and how do you express "no actions"?
Show answer
Use an empty list: expectedActions: []. The expected topic depends on your agent. A typical choice is a general or greeting topic, such as General_FAQ, and you must use the developer name from your own agent. A complete case: utterance "Hi there", expectedTopic General_FAQ, expectedActions [], expectedOutcome "The agent greets the customer and offers to help with orders."