2015-07-04 19 views
5

Sadece basit takip eden denetleyici eylemi parola bütünleştirme testi ile. İşte benim testim.Grails3 denetleyici tümleştirmesi sınaması başarısız: Hayır iş parçacığına bağlı istek bulunamadı

@Integration 
@Rollback 
class TestControllerSpec extends Specification { 

    def setup() { 
    } 

    def cleanup() { 
    } 

    void "test something"() { 
     setup: 
     def c = new TestController() 
     c.index() 
     expect: 
     c.response.contentType !=null 
    } 
} 

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) 
    at grails.web.api.WebAttributes$Trait$Helper.currentRequestAttributes(WebAttributes.groovy:45) 
    at grails.web.api.ServletAttributes$Trait$Helper.getRequest(ServletAttributes.groovy:42) 
+0

sizi: alanına ekleyin

:

@Autowired WebApplicationContext ctx 

setup() yılında: GrailsWebMockUtil.bindMockWebRequest(ctx)

cleanup() yılında – dmahapatro

+0

@dmahapatro Sorumu güncelledim. – user1071671

cevap

4

Ne yazık ki test denetleyicileri için entegrasyon testleri kullanamazsınız, Grails 3'te bir sınırlama olabilir şu İstisna alma.

Test denetleyicilerinin entegrasyonu için, bir Geb işlev testi oluşturmak için create-functional-test komutunu kullanmanız önerilir.

Source from Grails documentation

Bu grails önceki sürümlerinde yönde önemli bir değişiklik gibi görünüyor. Eğer gerçekten bir entegrasyon testinde bir denetleyici test etmek gerekiyorsa, bunu deneyin olabilir:

NOT: Bunun kötü bir uygulama olabilir gerçekleştirmek ve Grails belgelere aykırı, ama bazen de test etmek gerek Daha fazla programatik olarak, birim testleri yeterli değildir ve Geb testleri yeterince ayrıntılı değildir.

@TestFor(TestController) // This will provide a mocked "controller" reference 
@Integration 
@Rollback 
class TestControllerSpec extends Specification { 

    // If TestController uses any services, have them autowired into this test 
    @Autowired 
    SomeService someService 

    def setupSpec() { 
     // Now connect those services to the controller 
     controller.someService = someService 
    } 

    void "test something"() { 
     when: 
     controller.index() 

     then: 
     response.contentType != null 
    } 
} 

UYARI: Bir sorun buldunuz bu biçimde bazı ek işten sonra. @TestFor kullanarak, Holders.clear() numaralı çağrıyı tamamlayacaksınız, yani Holders'da grailsApplication nesnesi olmayacaktır. Yukarıdaki yaklaşımı kullanan birinden sonra çalışan tümleştirme testleri varsa, bu sorunlara neden olur. Çok fazla kazma işleminden sonra, bu işi yapmanın kolay (hatta zor) bir yolu var gibi görünmüyor, ki bu da muhtemelen Grails 3'te desteklenmiyor. Bu söyleniyor ki, bir seçenek diğer bütünleştirme testlerini @TestFor, böylece Holders sınıfı uygun şekilde doldurulacak. Bu bir hack mi? Evet öyle! Tüm testlere bu ek yükü ekleme çabalarına değip değmeyeceğine karar vermeniz gerekecektir. Benim durumumda buna ihtiyaç duyulan sadece bir başka entegrasyon testi (küçük bir uygulama olduğu için), fakat bundan daha fazlası olsaydı bu yaklaşımı kullanmazdım. Can RequestContextHolder.resetRequestAttributes()

9

Bunu yapıyorum ve iyi iş gibi görünüyor Tam test sınıfını ve kontrol altındaki test cihazını q'a ekleyin. aynı zamanda?
İlgili konular