模拟一个具有双指针作为参数的函数

时间:2017-12-13 14:22:51

标签: c++ gmock

模拟一个具有原始双指针的方法,例如,在类Helper下面有一个方法int run(int ** a)。我试图使用SetArgPointee设置期望,但它无法正常工作。给编译器错误无法将int ** const转换为int *。

func downloadVideo()   
{
    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
        var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!

        let pathComponent = "\(String(describing: documentsURL))/tempFile.mp4"

        documentsURL?.appendPathComponent(pathComponent)

        return (documentsURL!, [.createIntermediateDirectories, .removePreviousFile])
    }

    Alamofire.download(self.downloadUrl, to: destination).responseData { response in
        if response.destinationURL == nil{

            self.alertMessage2(titleText: "Error", messageText: "No Video URL", style: .actionSheet, actionTitle: "OK", actionStyle: .default , handler: {(action) in self.performSegue(withIdentifier: self.home, sender: self)})
            print("no url ")
            //
        }else{

            print("succes ")
            PHPhotoLibrary.shared().performChanges({
                PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: response.destinationURL!)

            }) { saved, error in
                if saved { self.alertMessage2(titleText: "Your video was successfully saved", messageText: "", style: .alert, actionTitle: "OK", actionStyle: .default , handler: {(action) in self.performSegue(withIdentifier: self.home , sender: self)})
                }else {print("fel", error!)}}
        }
        }
        .downloadProgress { progress in
            self.progressView.progress = Float(progress.fractionCompleted)
            print("Progress: \(progress.fractionCompleted)")
    }
}

我无法用test2替换测试双指针。想知道怎么做。

1 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,但我会发布,因为评论太长了。

首先,您应该解释一下您想要实现的目标,因为通常没有理由设置被调用的模拟函数参数(至少,如果您不打算使用修改过的arg执行任何其他操作)。 / p>

SetArgPointee只允许您将值设置为a[0],因为这是指针所指向的内存:

int** mockInput1 = new int*[2];
int** mockInput2 = new int*[2];
EXPECT_CALL(helper, run(_))
    .Times(1)
    // this will set value pointed by mockInput2[0] to mockInput1[0]
    .WillOnce(DoAll(SetArgPointee<0>(mockInput1[0]), Return(99)));
helper.run(mockInput2);

但是,我几乎可以肯定这不是你想要的。请注意,gmock允许您定义在匹配呼叫后可以调用的自定义操作:

auto action = [](int** a) {
    int** test = new int*[2];
    test[0] = new int[1];
    test[0][0] = 5;
    test[1] = new int[1];
    test[1][0] = 55;
    a[0] = test[0];
    a[1] = test[1];  // or whatever
    std::cout << "Executed custom action" << std::endl;
};
EXPECT_CALL(helper, run(_))
    .Times(1)
    .WillOnce(DoAll(Invoke(action), Return(99)));
helper.run(mockInput2);

在动作中你可以做任何你想做的事。无论如何,试着解释你实际上想要实现的目标。

此外,如果您没有正确删除内存,请不要忘记内存泄漏。