数字匹配器错误,即使值是数字类型
Number Matcher Error even though values are of type Number
我的描述案例中的第三个测试案例有问题。更具体地说,我收到行 expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1)
的错误。在这里,我不断收到“匹配器错误:接收到的值必须有一个长度 属性,其值必须是一个数字”错误。我使用了 typeof 函数,它发现我的接收值和期望值都是数字,但我仍然不断收到此错误。任何帮助将不胜感激。
代码
const supertest = require('supertest')
const mongoose = require('mongoose')
const app = require('../app')
const Blog = require('../models/blog')
const { initial } = require('lodash')
const api = supertest(app)
const initalBlogs = [
{
title: "React patterns",
author: "Michael Chan",
url: "https://reactpatterns.com/",
likes: 7
},
{
title: "Go To Statement Considered Harmful",
author: "Edsger W. Dijkstra",
url: "http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html",
likes: 5
}
]
beforeEach(async () => {
await Blog.deleteMany({})
let newBlog = new Blog(initalBlogs[0])
await newBlog.save()
newBlog = new Blog(initalBlogs[1])
await newBlog.save()
})
describe('blog tests', () => {
test('returns blog list length', async () => {
const blogs = await api.get('/api/blogs')
const result = blogs._body
expect(result.length).toBe(2)
})
test('verify unique identifier is named id', async () => {
const blogs = await api.get('/api/blogs')
const result = blogs._body
result.forEach((element) => {
expect(element.id).toBeDefined()
})
})
//ISSUES HERE
test('adds new blog', async () => {
const newBlog = {
title: "Node patterns",
author: "Cool Chan",
url: "https://fregrferfref.com/",
likes: 7
}
const result = await api.post('/api/blogs').send(newBlog).expect(201)
const secondResult = await api.get('/api/blogs')
const filteredArr = secondResult._body.map(a => a.title);
expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1) //Says the length property value should be a number even though it is a number
expect(filteredArr).toContain('Node patterns')
})
})
afterAll(() => {
mongoose.connection.close()
})
在这一行
expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1)
你计算了两次长度:一次用 .length
一次用 .toHaveLength()
.
换句话说,您正在测试 secondResult._body.length.length
的值,这会引发错误,因为 secondResult._body.length
是一个数字,因此它的长度未定义。
尝试
expect(secondResult._body).toHaveLength(initalBlogs.length + 1)
或
expect(secondResult._body.length).toBe(initalBlogs.length + 1)
我的描述案例中的第三个测试案例有问题。更具体地说,我收到行 expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1)
的错误。在这里,我不断收到“匹配器错误:接收到的值必须有一个长度 属性,其值必须是一个数字”错误。我使用了 typeof 函数,它发现我的接收值和期望值都是数字,但我仍然不断收到此错误。任何帮助将不胜感激。
代码
const supertest = require('supertest')
const mongoose = require('mongoose')
const app = require('../app')
const Blog = require('../models/blog')
const { initial } = require('lodash')
const api = supertest(app)
const initalBlogs = [
{
title: "React patterns",
author: "Michael Chan",
url: "https://reactpatterns.com/",
likes: 7
},
{
title: "Go To Statement Considered Harmful",
author: "Edsger W. Dijkstra",
url: "http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html",
likes: 5
}
]
beforeEach(async () => {
await Blog.deleteMany({})
let newBlog = new Blog(initalBlogs[0])
await newBlog.save()
newBlog = new Blog(initalBlogs[1])
await newBlog.save()
})
describe('blog tests', () => {
test('returns blog list length', async () => {
const blogs = await api.get('/api/blogs')
const result = blogs._body
expect(result.length).toBe(2)
})
test('verify unique identifier is named id', async () => {
const blogs = await api.get('/api/blogs')
const result = blogs._body
result.forEach((element) => {
expect(element.id).toBeDefined()
})
})
//ISSUES HERE
test('adds new blog', async () => {
const newBlog = {
title: "Node patterns",
author: "Cool Chan",
url: "https://fregrferfref.com/",
likes: 7
}
const result = await api.post('/api/blogs').send(newBlog).expect(201)
const secondResult = await api.get('/api/blogs')
const filteredArr = secondResult._body.map(a => a.title);
expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1) //Says the length property value should be a number even though it is a number
expect(filteredArr).toContain('Node patterns')
})
})
afterAll(() => {
mongoose.connection.close()
})
在这一行
expect(secondResult._body.length).toHaveLength(initalBlogs.length + 1)
你计算了两次长度:一次用 .length
一次用 .toHaveLength()
.
换句话说,您正在测试 secondResult._body.length.length
的值,这会引发错误,因为 secondResult._body.length
是一个数字,因此它的长度未定义。
尝试
expect(secondResult._body).toHaveLength(initalBlogs.length + 1)
或
expect(secondResult._body.length).toBe(initalBlogs.length + 1)