问题描述
我正在学习使用 mocha 和 chai 为节点应用程序编写测试用例,我编写了以下测试用例
let chai = require('chai');
let chaiHttp = require('chai-http');
const should = chai.should;
const expect = chai.expect;
const server = "http:\\localhost:3000"
chai.use(chaiHttp);
describe('Create Login and Register',() => {
it('should login using credentials',() => {
chai.request(server)
.get('/register')
.send()
.then((res: any) => {
res.should.have.status(200);
done();
}).catch((err: any) => { done(err) })
})
})
我需要为它添加一些类型才能工作我缺少什么吗,我尝试再次安装 chai-http 但仍然是同样的问题
解决方法
var controllerActionList = assembly.GetTypes()
.Where(type => typeof(Controller).IsAssignableFrom(type))
.SelectMany(type => type.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public))
.Select(x => new
{
Area = x.DeclaringType?.CustomAttributes.Where(c => c.AttributeType == typeof(AreaAttribute)),Controller = x.DeclaringType,Action = x
}).ToList();
foreach (var item in controllerActionList)
{
var controleerDisplayName = item.Controller.GetTypeInfo().GetCustomAttribute<DisplayNameAttribute>()?.DisplayName;
}
作为测试函数的第一个参数传入。
done
虽然,因为您使用的是 describe('Create Login and Register',() => {
it('should login using credentials',(done) => { // <-- here
chai.request(server)
.get('/register')
.send()
.then((res: any) => {
res.should.have.status(200);
done();
}).catch((err: any) => { done(err) })
})
})
链,所以您应该只返回该链。
Promise