我在一个名为button-actions的模块中有一个函数,当用户单击后退按钮时会调用该函数。我想直接测试backButtonActions方法,但需要在被调用的backButtonActions中模拟leaveApp和displayById方法。
这是我的 button-actions.js 文件方法。
public static void detectSgPoints(int blkSze, Mat src) {
utils.console2file("cons_2_file");
int windowX = 1;
int windowY = 1;
if (blkSze < src.width()) {
windowX = src.width() - blkSze;
}
if (blkSze < src.height()) {
windowY = src.height() - blkSze;
}
Map<Point, Double> map = new HashMap<>();
double[][] avg = new double[src.height() / blkSze][src.width() / blkSze];
int m = 0;
int n = 0;
for (int i = 0; i < windowY; i += blkSze) {
for (int j = 0; j < windowX; j += blkSze) {
Mat block = utils.getROI(src, new Rect(j, i, blkSze, blkSze));
Mat dx = new Mat(new Size(blkSze, blkSze), CvType.CV_64FC1);
Mat dy = new Mat(new Size(blkSze, blkSze), CvType.CV_64FC1);
Imgproc.Sobel(block, dx, CvType.CV_64FC1, 1, 0);
Imgproc.Sobel(block, dy, CvType.CV_64FC1, 0, 1);
Mat orientation = calculateOrientation(dx, dy);
int cpx = j + (blkSze / 2), cpy = i + (blkSze / 2);
avg[m][n] = avgAngle(orientation, false);
if (avg[m][n] < 0) {
avg[m][n] = 360 + avg[m][n];
}
map.put(new Point(cpx, cpy), avg[m][n]);
n++;
}
n = 0;
m++;
}
for (int mm = 1; mm < avg.length - 1; mm++) {
for (int nn = 1; nn < avg[0].length - 1; nn++) {
int j = nn * blkSze;
int i = mm * blkSze;
double psum = 0;
int cpx = j + (blkSze / 2), cpy = i + (blkSze / 2);
for (int k = 0; k < anglePos2.length - 1; k++) {
double dif = 0.0;
dif = avg[mm + anglePos2[k + 1][0]][nn + anglePos2[k + 1][1]]
- avg[mm + anglePos2[k][0]][nn + anglePos2[k][1]];
System.out.println("adding " + "(" + avg[mm + anglePos2[k +1[0]][nn + anglePos2[k + 1][1]] + "-"
+ avg[mm + anglePos2[k][0]][nn + anglePos2[k][1]] + ") = " + dif + " to " + psum);
psum = psum + dif;
}
double poincare = psum;
System.out.println("cpx = " + cpx + ", cpy = " + cpy + " poincare = " + poincare);
}
}
}
private static double avgAngle(Mat orientation, boolean toDegrees) {
List<Double> angle = new ArrayList<>();
for (int i = 0; i < orientation.height(); i++) {
for (int j = 0; j < orientation.width(); j++) {
double value = orientation.get(i, j)[0];
value = Math.toDegrees(value);
angle.add(value);
}
}
return getMeanAngle(angle);
}
public static double getMeanAngle(List<Double> sample) {
double x_component = 0.0;
double y_component = 0.0;
double avg_d, avg_r;
for (double angle_d : sample) {
double angle_r;
angle_r = Math.toRadians(angle_d);
x_component += Math.cos(angle_r);
y_component += Math.sin(angle_r);
}
x_component /= sample.size();
y_component /= sample.size();
avg_r = Math.atan2(y_component, x_component);
avg_d = Math.toDegrees(avg_r);
return avg_d;
}
public static Mat calculateOrientation(Mat dx, Mat dy) {
Mat orientation = new Mat(dx.size(), CvType.CV_32F);
for (int i = 0; i < dx.rows(); i++) {
for (int j = 0; j < dx.cols(); j++) {
double valueX = dx.get(i, j)[0];
double valueY = dy.get(i, j)[0];
double result = Math.atan2(valueY, valueX);
orientation.put(i, j, result);
}
}
return orientation;
}
我是测试的新手,所以我可能会遗漏一些非常简单的东西。 以下是我的 test.js 文件
export function backButtonActions( label, page ){ //, page
console.log("LABEL = ", label, " page = ", page);
switch( label ){
case 'step1':
page.leaveApp();
break;
case 'step2':
page.displayById();
break;
}
}
当我运行上述测试时,我收到以下错误。
●按钮操作> backButtonActions需要一个字符串 “第1步”或“第2步”
window.$ = require('jquery'); import {backButtonActions} from '../button-actions'; describe('Button Actions', () => { const page = {} beforeEach(() => { page.leaveApp = jest.fn(() => "leave"); page.displayById = jest.fn(() => "Display"); document.body.innerHTML = '<div>' + ' <button class="btn-back" />' + '</div>'; $('.btn-back').click((event, label) =>{ backButtonActions( label, page ); }); }); it('backButtonActions requires a string of either "step1" or "step2"', () => { $('.btn-back').trigger('click', 'step1'); expect(backButtonActions).toBeCalled(); expect(backButtonActions).toBeCalledWith("step1" || "step2"); }); })
我还应该运行其他什么才能让它发挥作用吗?
答案 0 :(得分:5)
您需要通过jasmine的backButtonActions
方法或通过Jest的spyOn
方法监视jest.spyOn
函数
https://facebook.github.io/jest/docs/jest-object.html#jestspyonobject-methodname