Node.js 如何自动测试用户旅程?

Node.js 如何自动测试用户旅程?,node.js,mocha.js,Node.js,Mocha.js,我使用NodeJS作为web服务器,使用Mocha进行测试。我尝试了此测试以确保应用程序可以创建用户、注销并再次登录: const request=require('supertest'); 常量app=require('../app'); const req=请求(应用程序); 描述(“用户旅程”,函数(){ 它(“测试用户旅程(注册、注销、登录)”,功能(完成){ 让SignedStub=“仅限登录用户使用”; 请求 .post(“/注册”) .发送({ “电子邮件”:test@test.c

我使用NodeJS作为web服务器,使用Mocha进行测试。我尝试了此测试以确保应用程序可以创建用户、注销并再次登录:

const request=require('supertest');
常量app=require('../app');
const req=请求(应用程序);
描述(“用户旅程”,函数(){
它(“测试用户旅程(注册、注销、登录)”,功能(完成){
让SignedStub=“仅限登录用户使用”;
请求
.post(“/注册”)
.发送({
“电子邮件”:test@test.com",
“密码”:“abcd1234”
})
.expect(功能(响应){
expect(response.body.includes(signedistub))
})
.get(“/logout”)
.expect(function(response){//创建并跨多个请求使用该函数来持久化会话

如果需要,使用
npm install--save chai
安装
chai
,它与
supertest
一起工作(有关使用摩卡和chai在NodeJS中进行测试的详细信息,请参阅。)

如果使用的是Cookie以外的身份验证类型,请以相同的方式将数据持久化到变量中,并随每个请求一起发送

const request = require('supertest');
const app = require('../app');

const { expect } = require('chai')

const User = require('../models/user');
const email = "test@test.com";

describe("user journey", function() {

  let req
  let signedInStub = "something only for logged in users"

  before(function(){
    req = request.agent(app)
  })

  it("should signup a new test@test.com user", async function() {

    // Delete this email in case emails are unique in the database.
    await User.deleteOne({email: email});

    const response = await req.post("/signup")
      .send({
        "email": email,
        "password": "abcd1234"
      })
      .redirects(1); // optional, in case your back-end code uses a redirect after signing up.
    // Log the response so you can track errors, e.g. hidden parameters in the HTML form that are missing from this POST request in code.
    console.log(response.text);
    expect(response.text).to.include(signedInStub)
  })

  it("should logout the new user", async function() {
    const response = await req.get("/logout")
    expect(response.text).not.to.include(signedInStub)
  })

  it("should login the new user", async function() {
    const response = await req.post("/login")
      .send({
        "email": email,
        "password": "abcd1234"
      })
    expect(response.text).to.include(signedInStub)
  })

  it("should get the new users content", async function() {
    await req.get("/content/2")
      .expect(200)
  });
});

这是在使用supertest吗?我不相信它能以这种方式链接多个请求。是的,它在使用supertest,我更新了问题中的代码。我得到了
断言错误:被测试的对象必须是数组、映射、对象、集合、字符串或弱集,但在前三次测试中给出了对象,并且
错误:预期200“OK”,已找到302个“已找到”
在最后一个例子中,因为如果用户没有登录,我会重定向到404,而不是呈现模板。现在,测试线程没有终止,我需要用
Ctl-C
停止它,为什么会这样?不存在通常是指维护连接的东西,比如数据库或者可能是应用程序的代理。
mocha-e将强制退出,但您通常可以使用
after
功能关闭/断开连接。并检查
response
response.body
为这些测试设置的内容。我在
expect()之后添加了
console.log(response);
我在控制台中什么也没看到。原因是测试失败并在那一行之前抛出错误,所以我编辑了问题以记录失败断言之前的响应。现在我知道为什么测试保持了陈旧的连接:我开始使用
摩卡
从脚本运行测试,而不使用
--exit
。Becau由于测试函数是异步的,我发现退出命令行比从NodeJS应用程序中重复连接到Mongoose的代码更可靠(例如,
const-Mongoose=require('Mongoose');const-connectRetry=function(){Mongoose.connect('mongodb://localhost/db“,{…}};
const request = require('supertest');
const app = require('../app');

const { expect } = require('chai')

const User = require('../models/user');
const email = "test@test.com";

describe("user journey", function() {

  let req
  let signedInStub = "something only for logged in users"

  before(function(){
    req = request.agent(app)
  })

  it("should signup a new test@test.com user", async function() {

    // Delete this email in case emails are unique in the database.
    await User.deleteOne({email: email});

    const response = await req.post("/signup")
      .send({
        "email": email,
        "password": "abcd1234"
      })
      .redirects(1); // optional, in case your back-end code uses a redirect after signing up.
    // Log the response so you can track errors, e.g. hidden parameters in the HTML form that are missing from this POST request in code.
    console.log(response.text);
    expect(response.text).to.include(signedInStub)
  })

  it("should logout the new user", async function() {
    const response = await req.get("/logout")
    expect(response.text).not.to.include(signedInStub)
  })

  it("should login the new user", async function() {
    const response = await req.post("/login")
      .send({
        "email": email,
        "password": "abcd1234"
      })
    expect(response.text).to.include(signedInStub)
  })

  it("should get the new users content", async function() {
    await req.get("/content/2")
      .expect(200)
  });
});