English 中文(简体)
如何模拟新经营者
原标题:How to mock the new operator

I m测试一些使用java图书馆的农夫法,我希望通过使用该网络冲破图书馆的电话。 因此,正在测试的法典看起来像:

def verifyInformation(String information) {
    def request = new OusideLibraryRequest().compose(information)
    new OutsideLibraryClient().verify(request)
}

我尝试使用MockFor和StubFor,但我发现以下错误:

No signature of method: com.myproject.OutsideLibraryTests.MockFor() is applicable for argument types: (java.lang.Class) values: [class com.otherCompany.OusideLibraryRequest]  

I m 使用Grails 2.0.3。

最佳回答
问题回答

我刚刚发现,我们总是能够通过MetaClass推翻建筑商,因为2号铁路将在每次试验结束时重新确定MetaClass的修改。

这一trick比格罗诺夫y sMockFor好。 AFAIK, Groovy s MockFor 不允许我们修饰JDK的课堂,例如java.io.File。 但是,在以下例子中,您不能使用<代码>File file = 新的文档(“aa”),因为真正的物体类型是<代码>Map,而不是<代码>File。 例子就是斯波克特。

def "test mock"() {
    setup:
    def fileControl = mockFor(File)
    File.metaClass.constructor = { String name -> [name: name] }
    def file = new File("aaaa")

    expect:
    file.name == "aaaa"
}




相关问题
UserControl constructor with parameters in C#

Call me crazy, but I m the type of guy that likes constructors with parameters (if needed), as opposed to a constructor with no parameters followed by setting properties. My thought process: if the ...

Strange "type class::method() : stuff " syntax C++

While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass() : pimpl_( new MyClassImp() ) First: What does it mean? Second: What is the syntax? Sorry for being such ...

Anonymous class question

I ve a little doubt over this line: An anonymous class cannot define a constructor then, why we can also define an Anonymous class with the following syntax: new class-name ( [ argument-list ] ) {...

Why copy constructor is not called in this case?

Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other....

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

热门标签