- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
因此,我尝试在我的 Express 应用程序中测试一条路线,为此,我需要在调用电话之前登录用户
。我在 beforeEach
函数中创建并保存一个 user
。这是我正在编写的测试:
it('should update username', function(done){
var _this = this;
req.post('/login')
.send(_this.data)
.then(function(res){
req.put('/users/' + res.body.user._id)
.send({ username: 'robert'})
.expect(200)
.end(function(err, res){
if(err) return done(err);
console.log(res.body);
res.body.success.should.equal(true);
res.body.user.username.should.match(/robert/);
done();
});
});
});
这是我运行测试时得到的输出:
Users
Routes
Authenticated
POST /login 200 195.059 ms - 142
PUT /users/568a432e1daa24083fa6778a 401 2.785 ms - 21
1) should update username
Unauthenticated
GET /users 401 1.502 ms - 21
✓ should return 401
1 passing (516ms)
1 failing
1) Users Routes Authenticated should update username:
Error: expected 200 "OK", got 401 "Unauthorized"
at Test._assertStatus (node_modules/supertest/lib/test.js:232:12)
at Test._assertFunction (node_modules/supertest/lib/test.js:247:11)
at Test.assert (node_modules/supertest/lib/test.js:148:18)
at Server.assert (node_modules/supertest/lib/test.js:127:12)
at net.js:1273:10
我很困惑为什么当 POST/login
请求响应 200
时,它却以 401
响应。
使用Postman
我能够创建一个用户
,以该用户身份登录,并且通过PUT
请求我能够成功更新数据。因此,我假设这与 supertest
的 req
链接有关。
我已经使用 supertest-as-promised
以及 supertest
编写了请求链。
据我了解,以下代码的行为与使用 then()
语法相同:
it('should update username', function(done){
var _this = this;
req.post('/login')
.send(_this.data)
.endfunction(err, res){
if(err) return done(err);
req.put('/users/' + res.body.user._id)
.send({ username: 'robert'})
.expect(200)
.end(function(err, res){
if(err) return done(err);
console.log(res.body);
res.body.success.should.equal(true);
res.body.user.username.should.match(/robert/);
done();
});
});
});
我对这里发生的事情感到困惑。就像我说的,我可以使用 Postman 来做到这一点,所以我认为这是请求链如何工作的问题。如果您需要更多上下文,我可以根据需要提供更多代码。
最佳答案
解决方案就像更改一样简单
var req = require('supertest-as-promised')(app);
至
var req = require('supertest-as-promised').agent(app);
调用supertest.agent
允许supertest
作为网络 session 工作并在链接请求时保留 session 、cookie 和 header 。
关于node.js - Mocha 测试,链接 super 测试请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34589011/
问题比较Java,但是我想在Android中实现: 假设有 3 个或更多类相互扩展: class A { ... int color; ... } class B extend
我知道标题听起来有点奇怪,但这正是我愿意做的。简单解释:A类是B类的子类,B类也是C类的子类>. 现在,所有这些类都包含方法m()。在我的 A 类中,这是我唯一可以访问的类,因为其他类仅在运行时可用,
我有一个 UIViewController 类 A 和 B。 A 使用以下方式加载 B:[A.view addSubView B.view]。 B 有一个带有“后退”按钮的导航栏。我想在单击时返回到
我有以下(第三方)类结构。我们将调用第三方项目 ProjectSeriously,并注意我使用 System.out.println 代替其他复杂的功能(100 行代码) . class A {
在下面的代码中,我从 Game 扩展了 MyGame。我有两个问题: 我们是否需要为所有render()、dispose()、pause()调用super方法 和 resize(w,h)?很多人都没有
例如,假设我想在调用 super.viewDidLoad() 时跳过一级。所以我希望能够做这样的事情: override func viewDidLoad() { super.super.vi
public class Faculty extends Employee { public static void main(String[] args) { new Fac
假设我有: class Superclass { //fields... methodA() {...} methodB() {...} ... } class Subclass exte
这个问题在这里已经有了答案: Why is super.super.method(); not allowed in Java? (22 个答案) 关闭 9 年前。 我怀疑我想做的事情是否可行。我有
我有一个实现 Initializable 的类。 public abstract class ExampleClass implements Initializable { public vo
我想知道,我有这个大数组,是否可以只在内存中使用一次而不是每个线程一次?以 stackoverflow 上的标签为例。他们几乎从不改变,为什么不为他们留下一个内存点呢?甚至可能将该数组永久保存在内存中
假设这三个类具有这个简单的层次结构: class A { func foo() { print("A") } } class B: A { override fu
有没有办法在 TypeScript 中调用 super.super.methodName。我想避免调用super.methodName,但我想调用二祖的methodName方法。 谢谢。 最佳答案 T
这个问题已经有答案了: When do I use super()? (11 个回答) 已关闭 7 年前。 package Geometry; public abstract class Geomet
我必须执行and()在我的实现 Predicate 的业务对象上. 出现问题的代码是 and() 行调用: Predicate predicate = new M
我有一个实现接口(interface)的抽象父类(super class): public abstract class FooMatrix implements Matrix { publi
我有四个 UIView:viewA 是 Root View ,它有 viewB 作为它的 subview 。 viewB 将 viewC 作为其 subview ,而 viewC 将 viewD 作为
有什么区别: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() 和:
我有一个通用接口(interface) interface ListList extends List> .由于某些原因,我无法转换 ListList至 List> .有什么方法可以做到吗?为什么它不
我想调用带有两个参数的父类(super class)的构造函数,所以我调用了 super(arguments),但是编译器说: “类 Person 中的构造函数 Person 不能应用于给定类型; 要
我是一名优秀的程序员,十分优秀!