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 查詢、憑據等。
添加回答
舉報
