1 回答
TA贡献1943条经验 获得超7个赞
在剧本模式中,交互并不意味着返回值,只是为了在被测系统上执行一些操作。您使用问题来查询系统的状态。如果您分享您尝试解决的整体问题会更容易,但由于您的示例是关于查询用户的,您可以进行测试来检查您是否可以通过 UI 管理页面将用户添加到系统:
@Test
public void add_a_user_to_the_system() {
Actor ada = Actor.named("Ada").describedAs("an admin");
when(ada).attemptsTo(
AddANewUser.called("Jack")
);
then(ada).should(
seeThat(KnownUsers.inTheSystem(),
contains(hasProperty("name", equalTo(("Jack"))))
)
);
}
(在这种情况下,按名称搜索用户可能会更有效,但我想让它接近您的示例)。
为此,您可能有一个AddANewUser使用管理屏幕添加新用户的类:
class AddANewUser implements Performable {
public static Performable called(String userName) {
return instrumented(AddANewUser.class, userName);
}
private final String userName;
AddANewUser(String userName) {
this.userName = userName;
}
@Override
public <T extends Actor> void performAs(T actor) {
// Add a new user called userName via the UI
}
}
然后你会使用一个问题来检查这个用户是否存在:
@Subject("known users")
static class KnownUsers implements Question<List<ApplicationUser>> {
public static KnownUsers inTheSystem() { return new KnownUsers(); }
@Override
public List<ApplicationUser> answeredBy(Actor actor) {
// Query the database and convert the result set to ApplicationUsers
return ...;
}
}
您还可以创建一个 Ability 类来集中 JDBC 查询、凭据等。
添加回答
举报
