如何使用 jasmine-node 和 RequireJS 正确运行 jasmine 测试?
我已经尝试过这样的事情,但不起作用(CoffeeScript):
requirejs = require 'requirejs'
requirejs.config { baseUrl: __dirname + '/../' }
requirejs ['MyClasses', 'FooClass'], (MyClasses, FooClass) ->
describe "someProp", ->
it "should be true", ->
expect(MyClasses.FooClass.someProp).toEqual true
Run Code Online (Sandbox Code Playgroud)
0 秒内完成 0 次测试、0 次断言、0 次失败
我的目标是使用 RequireJS、CoffeeScript 编写模块化类,并且类必须可以使用 jasmine-node(CI 服务器)进行测试。
请问我该怎么做?谢谢!
编辑:
我使用命令执行测试(在带有测试的目录中):
jasmine-node ./
Run Code Online (Sandbox Code Playgroud) onSaveEvent: function (event) {
if (this.model !== null) {
var that = this;
this.model.save(this.model.toJSON(), {
success: function (model) {
that.model = model;
that.model.attributes.isDirty = false;
},
error: function (model, xhr) {
that.model.attributes.isDirty = true;
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何对模型进行单元测试,以保存Jasmine中的成功和错误响应?
我有一个用coffeescript编写的准系统规范:
# test/foobar.spec.coffee
describe "falsy test", ->
it "should fail", ->
expect(true).toBe false
Run Code Online (Sandbox Code Playgroud)
当我jasmine-node --coffee test/foobar.spec.coffee从项目目录运行时,我收到以下错误:
Exception loading: /Users/myuser/programming/project/test/foobar.spec.coffee
{ [Error: Cannot find module '/Users/myuser/programming/project/test/foobar.spec'] code: 'MODULE_NOT_FOUND' }
Run Code Online (Sandbox Code Playgroud)
我在用:
node --version
v0.10.8
jasmine-node --version
1.13.0
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会这样?
每次我保存一些文件时,我都会尝试运行测试.这是gulp手表:
gulp.task('jasmine', function() {
gulp.src('spec/nodejs/*Spec.js')
.pipe(jasmine({verbose:true, includeStackTrace: true}));
});
gulp.task('watch', function () {
gulp.watch(['app/*.js', 'app/!(embed)**/*.js','spec/nodejs/*.js'], ['jasmine']);
});
Run Code Online (Sandbox Code Playgroud)
要测试例如app/maps.js我正在创建一个spec/nodejs/mapsSpec.js文件,如下所示:
'use strict';
var maps = require('../../app/maps');
describe('/maps related routes', function(){
it('should ...', function(){...}
...
Run Code Online (Sandbox Code Playgroud)
如果我更改spec文件一切正常,如果我修改app/maps.js文件,则更改会触发测试.如果我再次修改它,测试会被触发,但修改不会生效.例如,如果我在第二次添加console.log('foo'),我将不会看到它,直到我重新启动gulp watch并再次保存它.因此,与gulp.watch一起使用时,只有一次茉莉花可以使用.
我想这是因为需要在gulp进程中由nodejs缓存.那我该怎么办?
我正在使用最新的istanbul + jasmine-node来编写我的所有测试脚本.
在一些地方,我有一个可读的流可能会发出error事件,我不知道如何在测试环境中模拟这样的事件,以提供代码覆盖.
请问有人建议如何处理这个问题吗?
我的应用程序使用的服务返回一个通常依赖于一大堆其他promise的promise.我已将其重构为单独的命名函数,以使测试(和可读性)更容易.所以在这种情况下,我只想测试run函数是否完成其工作并调用其他函数.
例如
run() {
return myService
.connection
.then(this.namedFunction1)
.then(this.namedFunction2)
.then(this.namedFunction3)
.catch((error) => {
console.log("doh!", error.stack);
});
Run Code Online (Sandbox Code Playgroud)
当我测试namedFunction1被称为Jasmine失败时,即使事实并非如此.这是一个我为保持简单而做的一个小代码示例:
getString() {
return Promise.resolve("Heeeelp. Heeeelp!!");
}
printToConsole(string) {
console.log(string); // This works! but Jasmine says nay :(
}
myFunction() {
this.getString()
.then(this.printToConsole)
.catch((error) => {
console.log("Some error occurred", error);
});
}
Run Code Online (Sandbox Code Playgroud)
......和测试:
it("should call the printToConsole function", function() {
spyOn(myClass, "printToConsole").and.callThrough(); //added the call through so it would print
myClass.myFunction();
expect(myClass.printToConsole).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)
和输出......
> Started F[2016-05-16 11:32:31.898] console - Heeeelp. Heeeelp!!
>
> …Run Code Online (Sandbox Code Playgroud) const Client = require('./src/http/client');
module.exports.handler = () => {
const client = new Client();
const locationId = client.getLocationId(123);
};
Run Code Online (Sandbox Code Playgroud)
我如何测试这个模块,断言client.getLocationId已经用123Jasmine 中的参数调用了?
我知道如何用诗乃实现这一点,但我对茉莉一无所知。
我正在尝试在当前目录上使用 bazel 运行 jasmine 测试,但它抱怨没有找到规格。
我认为这与我提供的“srcs”变量有关。我试过了[":spec/test.spec.ts"],[":spec"]但没有一个工作。
我使用的命令:bazel run //packages/core:unit_test
文件目录:
根
- 包
- 核
---规格
----test.spec.ts
jasmine_node_test(
name = "unit_test",
srcs = [":spec/test.spec.ts"],
deps = [
"@npm//jasmine"
],
)
Run Code Online (Sandbox Code Playgroud)
测试规范
describe("A suite is just a function", function() {
var a;
it("and so is a spec", function() {
a = true;
expect(a).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
包.json
"devDependencies": {
"@bazel/bazel": "^0.24.1",
"@bazel/buildifier": "^0.22.0",
"@bazel/ibazel": "^0.10.2",
"@bazel/typescript": "^0.28.0",
"@types/node": "^12.0.0",
"tslib": "^1.9.3",
"typescript": "^3.4.5"
},
"dependencies": {
"@bazel/jasmine": "^0.32.2", …Run Code Online (Sandbox Code Playgroud) 我正在用expressjs构建一个API,我的路由看起来像这样
module.exports = function(app){
var book = require('../controllers/book.controller');
app.get('/api/books', book.getBooks); //get all books
app.post('/api/books', book.addBook); //add a book
app.put('/api/book/:book_id', book.updateBook); //update a book entry
app.delete('/api/book/:book_id', book.deleteBook); //delete a book
}
Run Code Online (Sandbox Code Playgroud)
回调函数定义如下
var Book = require('../models/book.model');
module.exports = {
addBook: function(req, res){
Book.create(req.body, function(err, book){
if(err){
return res.json(400, err);
}
res.status(201).json(book);
});
},
getBooks: function(req, res){
Book.find({}).exec(function(err, books){
if(err){
return res.json(400, err);
}
res.status(201).json(books);
});
},
getOneBook: function(req, res){
Book.findById({_id: req.params.book_id}, function(err, book){
if(error){
return res.json(400, err)
}
res.status(201).json(book);
})
}, …Run Code Online (Sandbox Code Playgroud) 每当我尝试创建一个新项目时,karma.conf.js 总是丢失。有没有理由
我期望有 karma.conf.js 和所有其他东西,但我似乎找不到任何东西
项目文件丢失

jasmine-node ×10
jasmine ×5
node.js ×5
javascript ×2
angular ×1
backbone.js ×1
bazel ×1
coffeescript ×1
gulp ×1
istanbul ×1
jquery ×1
mocking ×1
node-modules ×1
requirejs ×1
stream ×1
supertest ×1
typescript ×1
unit-testing ×1