简体   繁体   English

环回模型的单元测试

[英]Unit testing for loopback model

I have a Loopback API with a model Student . 我有一个模型Student的Loopback API。

How do I write unit tests for the node API methods of the Student model without calling the REST API? 如何在不调用REST API的情况下为Student模型的节点API方法编写单元测试? I can't find any documentation or examples for testing the model through node API itself. 我找不到任何通过节点API本身测试模型的文档或示例。

Can anyone please help? 有人可以帮忙吗?

Example with testing the count method 测试count方法的示例

// With this test file located in ./test/thistest.js

var app = require('../server');

describe('Student node api', function(){
  it('counts initially 0 student', function(cb){
      app.models.Student.count({}, function(err, count){
        assert.deepEqual(count, 0);
      });
  });
});

This way you can test the node API, without calling the REST API. 这样您就可以在不调用REST API的情况下测试节点API。

However, for built-in methods, this stuff is already tested by strongloop so should pretty useless to test the node API. 但是,对于内置方法,这个东西已经通过strongloop测试,所以测试节点API应该没用。 But for remote (=custom) methods it can still be interesting. 但对于远程(=自定义)方法,它仍然很有趣。

EDIT: The reason why this way of doing things is not explicited is because ultimately, you will need to test your complete REST API to ensure that not only the node API works as expected, but also that ACLs are properly configured, return codes, etc. So in the end, you end up writing 2 different tests for the same thing, which is a waste of time. 编辑:这种做事方式没有说明的原因是,最终,您需要测试完整的REST API,以确保不仅节点API按预期工作,而且还要正确配置ACL,返回代码等所以最后,你最终为同一件事写了两个不同的测试,这是浪费时间。 (Unless you like to write tests :) (除非你喜欢写测试:)

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

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