我试图在这个控制器上进行单元测试。我是这种测试的新手,所以我不知道从哪里开始以及如何测试需要测试的东西,比如用户,渲染视图。)
class ShoppingCartController {
def shoppingCartWebshopService
def shoppingCartService
def springSecurityService
def checkoutOperaService
def databaseService
def dodAdresaService
def payingOrder(SetShop1Command cmd) {
def user = springSecurityService.isLoggedIn() ? springSecurityService.currentUser.sifpartnera : null
def tempAdrese = databaseService.getAdrese(user)
def adrese = []
tempAdrese.each{
adrese.add(it.adresa)
}
if (cmd.hasErrors()) {
render(view: 'OrderChoseAddress', model: [cmd : cmd, params : params, adrese: adrese ])
}
def inputUnAdrese = params.inputUnAdrese
[inputUnAdrese: inputUnAdrese, adrese: adrese]
}
这是我的测试
@Mock([SpringSecurityService,DatabaseService])
@TestFor(ShoppingCartController)
class ShoppingCartControllerTests {
void testPayingOrder(){
when:
// params.user = 19
// user.equals(id)
// println user
// controller.OrderChoseAddress()
controller.params.user = "admin"
controller.databaseService = new DatabaseService()
then:
assert model.adrese != null
}