Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 无法在Groovy测试中设置只读属性_Unit Testing_Groovy_Mocking - Fatal编程技术网

Unit testing 无法在Groovy测试中设置只读属性

Unit testing 无法在Groovy测试中设置只读属性,unit-testing,groovy,mocking,Unit Testing,Groovy,Mocking,大家好,我正在进行模拟测试,My class扩展了GroovyTestCase,我正在尝试在void设置方法中设置一个属性,如您所见: void setUp() { def slurper = new JsonSlurper() inData = slurper.parse( new File( "src/test/resources/fixtures/v1/watch/20160511_watch_listings_live_in.json" ), 'UTF-8' )

大家好,我正在进行模拟测试,My class扩展了GroovyTestCase,我正在尝试在void设置方法中设置一个属性,如您所见:

 void setUp()
{
    def slurper = new JsonSlurper()
    inData = slurper.parse( new File( "src/test/resources/fixtures/v1/watch/20160511_watch_listings_live_in.json" ), 'UTF-8' )
    outData = slurper.parse( new File( "src/test/resources/fixtures/v1/watch/20160511_watch_listings_live_out.json" ), 'UTF-8' )

    watchPresenter = BinderTestUtils.instanceForLibraryNamed( "dang_v1_watch_presenter" )
    watchPresenter.localTranslate = new LocalTranslateHelperTest( )
    //def info = [ mapper: mapperMock]
    //watchPresenter:[localTranslate:new LocalTranslateHelperTest( )]
    println("watchPresenterTranslate:" + watchPresenter.localTranslate.getStrings("en"))
}
但我得到了下一个错误:

无法为类WatchListingPresenterTests设置只读属性:localTranslate

您知道在这种情况下是否可以设置只读属性吗

在真实的类中,我使用localTranslate脚本,如下所示:

def strings = this.localTranslate.getStrings( params["lang"] )
我需要模拟这个属性,但是我得到了这个错误


提前感谢。

我使用get/setProperty元类解决了这个问题。关于运行时元编程的文档位于下一个链接中:

正如您所看到的,我的解决方案非常简单:

watchPresenter.metaClass .localTranslate = new LocalTranslateHelperTest( )
这很好地发挥了作用


无论如何,谢谢。

我使用get/setProperty元类解决了这个问题。关于运行时元编程的文档位于下一个链接中:

正如您所看到的,我的解决方案非常简单:

watchPresenter.metaClass .localTranslate = new LocalTranslateHelperTest( )
这很好地发挥了作用

无论如何,谢谢你