简体   繁体   English

使用酶进行浅渲染:实际和预期输出不匹配

[英]Shallow Rendering with enzyme: actual and expected output does not match

I am just writing code on a personal project for learning.我只是在个人项目上编写代码以供学习。

I have a class defined as below:我有一个定义如下的类:

import React from 'react';
export default class Counter{
    counterValue = 0;


    update(newValue){
        this.counterValue = newValue;
    }

    getValue(){
        return this.counterValue;
    }

    displayValue(){
        return <div>{this.getValue()}</div>
    }

}

I am trying to create a test on the displayValue() function.我正在尝试对displayValue()函数进行测试。 Code is below:代码如下:

import React from "react";
import Counter from "./Counter";
import Adapter from "enzyme-adapter-react-16";
import { shallow, mount, render, configure } from "enzyme";

configure({ adapter: new Adapter() });
var c1 = new Counter();
c1.update(188);

describe("check displayValue() method", () => {
  it("renders a div", () => {
    const wrapper = shallow(c1.displayValue());
    expect(wrapper.contains(<div>188</div>)).toBe(true);
  });
});

I used the command npm test and a 'react-scripts test' was executed.我使用命令npm test并执行了“反应脚本测试”。

The test fails.测试失败。 It tells me that它告诉我

expect(received).toBe(expected) // Object.is equality

Expected: true
Received: false

and the error is due to this line:错误是由于这一行:

expect(wrapper.contains(<div>188</div>)).toBe(true);

I have trouble understanding this and would appreciate advice.我很难理解这一点,希望得到建议。 Thank you!谢谢!

you need to add {} to the number like this:您需要将 {} 添加到数字中,如下所示:

import React from "react";
import Counter from "./Counter";
import Adapter from "enzyme-adapter-react-16";
import { shallow, mount, render, configure } from "enzyme";

configure({ adapter: new Adapter() });
var c1 = new Counter();
c1.update(188);

describe("check displayValue() method", () => {
  it("renders a div", () => {
    const wrapper = shallow(c1.displayValue());
    expect(wrapper.contains(<div>{188}</div>)).toBe(true);
  });
});

for more detail check here欲了解更多详情,请点击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM