簡體   English   中英

Grails單元測試控制器設置參數

[英]Grails unit testing controller setting up params

我有以下代碼的控制器:

def profile = Profile.findByProfileURL(params.profileURL)

和這樣的單元測試:

@TestMixin(GrailsUnitTestMixin)
@TestFor(ProfileController)
@Mock([User])
class ProfileControllerTests {

    def void testIndex() {
        mockDomain(User, [[firstname: 'Niko',...]])

        controller.params.profileURL = 'niko-klansek'

        controller.index()

        ...
    }

}

當我運行測試時,控制器中出現以下異常:

No signature of method: sportboard.core.profile.Profile.methodMissing() is applicable for argument types: () values: []

因此,在控制器中看不到我在測試中設置的參數值profileURL? 如何為控制器設置參數,使其可見?

異常是神秘的,但它表示沒有模擬您的Profile域類。 您應該將其添加到@Mock注釋中。 另外, @TestMixin可以省略@TestMixin,並且您不應該在測試中直接使用mockDomain 只需保存此用戶實例。 總共應該看起來像這樣:

@TestFor(ProfileController)
@Mock([User, Profile])
class ProfileControllerTests {
    def void testIndex() {
        def user = new User(firstName: 'Niko').save()

        controller.params.profileURL = 'niko-klansek'
        ...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM