English 中文(简体)
你们如何在格罗维耶的另一个物体内引爆物体?
原标题:How do you mock an object instantiated inside another object in Groovy?

这里我的例子

class CommandLine {

    def ls() {
        def cmd = "ls".execute()
        if(cmd.waitFor() != 0) {
            throw new Execution()
        }
        return cmd.text
    }
}

<代码>cmd, 变量为一物体。 进程。 为了测试被推翻的例外情况,我怎么会偏离waitFor()方法? 如果我能说的话,是否可以改写,以便利自动测试?

总的来说,你如何在另一类别内对一个物体进行模拟,或者你如何制定你的守则以允许测试?

最佳回答

答案是使用格罗莫夫大坝,而不是在Grails码头建造。

import groovy.mock.interceptor.MockFor

class TestClass {

    def test() {
        def mock = new MockFor(classToBeMocked)

        mock.demand.methodToBeMocked(1..1) { -> /* code */ }

        mock.use {
            /*
            All calls to objects of type classToBeMocked will be 
            intercepted within this block of code.
            */
        }
    }
}
问题回答

I m不熟悉格罗瓦尼,但如果有机会模拟以下方法:<代码>String#execute(,用process, 采用一种方法<<>waitFor(>,如果这种回归不会产生零结果,那将是这样做的途径。

类似:

Process mockedProcess = MockingFramework.mock(Process.class); // MockingFramework can be Mockito
MockingFramework.when(String.execute()).thenReturn(mockedProcess);
MockingFramework.when(mockedProcess.waitFor).thenReturn(1);

new CommandLine().ls(); // Boom! => Execution exception




相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...

热门标签