我有使用Jasmine的经验,并且非常喜欢它.有没有人有茉莉花和摩卡的经验,特别是对于Rails?我想知道是否值得转换.
我想为mocha设置一些默认值,而不必每次都输入它们.摩卡是否在任何地方寻找配置文件/ dotfile,因为jshint会查找.jshintrc并且npm会查找package.json?
我希望能找到一些帮助解决这个问题.我正在尝试为我正在编写的应用程序编写测试.我已将问题提炼到以下示例代码中.我想测试一个错误被抛出.我使用Testacular作为测试运行器,mocha作为框架,chai作为断言库.测试运行,但测试失败,因为抛出了错误!任何帮助是极大的赞赏!
function iThrowError() {
    throw new Error("Error thrown");
}
var assert = chai.assert,
    expect = chai.expect;
describe('The app', function() {
    describe('this feature', function() {
        it("is a function", function(){
            assert.throw(iThrowError(), Error, "Error thrown");
        });
    });
});
Run Code Online (Sandbox Code Playgroud) 我是Mocha的新手,我正在尝试用它来测试一个简单的React组件.如果react组件没有任何CSS样式,则测试将通过,但如果React组件中的标记包含任何className,则会抛出语法错误:
Testing.react.js
import React from 'react';
export default class Testing extends React.Component {
  render() {
    return (
      <section>
        <form>
          <input type="text" />
        </form>
      </section>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)
testing.jsx
import {
  React,
  sinon,
  assert,
  expect,
  TestUtils
} from '../../test_helper';
import TestingSample from '../../../app/components/Testing.react.js';
describe('TestingSample component', function(){
    before('render and locate element', function(){
        var renderedComponent = TestUtils.renderIntoDocument(
            <TestingSample />
        );
        var inputComponent = TestUtils.findRenderedDOMComponentWithTag(
            renderedComponent, 'input'
        );
        this.inputElement = inputComponent.getDOMNode();
    });
    it('<input> should be of type "text"', function () {
        assert(this.inputElement.getAttribute('type') === 'text'); …Run Code Online (Sandbox Code Playgroud) 我在rails中有一个导入控制器,它将多个带有多条记录的csv文件导入到我的数据库中.我想在RSpec中测试是否使用RSpec实际保存了记录:
<Model>.any_instance.should_receive(:save).at_least(:once)
Run Code Online (Sandbox Code Playgroud)
但是我得到错误说:
The message 'save' was received by <model instance> but has already been received by <another model instance>
Run Code Online (Sandbox Code Playgroud)
一个人为的控制器示例:
rows = CSV.parse(uploaded_file.tempfile, col_sep: "|")
  ActiveRecord::Base.transaction do
    rows.each do |row| 
    mutation = Mutation.new
    row.each_with_index do |value, index| 
      Mutation.send("#{attribute_order[index]}=", value)
    end
  mutation.save          
end
Run Code Online (Sandbox Code Playgroud)
是否可以使用RSpec进行测试或是否有解决方法?
我在我的测试模块中有一些调试器语句,并希望运行mocha并设置--debug-brk并点击我的断点,以便我可以检查模块的状态.不幸的是,每当我用这个选项运行mocha时,我最后会在下一行显示一个空白光标.我可以输入文本,但似乎没有任何东西可以处理我的命令(它当然不像节点调试器):
$ mocha --debug-brk tests.js -R spec
debugger listening on port 5858
[BLANK CURSOR]
Run Code Online (Sandbox Code Playgroud)
我是如何推出摩卡咖啡的?
我刚刚将shouldjs和mocha添加到我的Express应用程序进行测试,但我想知道如何测试我的应用程序.我想这样做:
app = require '../app'
routes = require '../src/routes'
describe 'routes', ->
  describe '#show_create_user_screen', ->
    it 'should be a function', ->
      routes.show_create_user_screen.should.be.a.function
    it 'should return something cool', ->
      routes.show_create_user_screen().should.be.an.object
Run Code Online (Sandbox Code Playgroud)
当然,该测试套件中的最后一个测试只是告诉med res.render函数(在show_create_user_screen中调用)是未定义的,可能是因为服务器没有运行且配置尚未完成.所以我想知道其他人如何设置他们的测试?
官方Mocha网站上的文档包含以下示例:
describe('User', function(){
  describe('#save()', function(){
    it('should save without error', function(done){
      var user = new User('Luna');
      user.save(function(err){
        if (err) throw err;
        done();
      });
    })
  })
})
Run Code Online (Sandbox Code Playgroud)
我想知道什么时候我应该在describe函数中嵌套我的测试,以及它的基本目的describe是什么.我可以将传递给的第一个参数与describe编程语言中的注释进行比较吗?describe控制台上的输出中未显示任何内容.它仅用于可读性目的,还是有其他用途用于此功能?
如果我像这样使用它有什么问题吗?
describe('User', function(){
    describe('#save()', function(){
        var user = new User('Luna');
        user.save(function(err){
            if (err) throw err;
            done();
        })
    })
})
Run Code Online (Sandbox Code Playgroud)
如果我这样做,测试仍然通过.
我有一个看起来像这样的Mocha测试文件:
var expect = require('chai').expect
var muting = require('../muting')
describe('muting', function () {
  describe('init()', function () {
    it('should inject an object into twitter', function () {
      var twitter = 'twitter'
      muting.init(twitter)
      expect(muting.twitter).to.equal(twitter)
    })
  })
})
Run Code Online (Sandbox Code Playgroud)
当我mocha从CLI运行时,它成功运行测试.
当我运行standard(JavaScript标准样式的可执行文件)时,我在Mocha的框架函数中遇到错误,如下所示:
standard: Use JavaScript Standard Style (https://github.com/feross/standard)   
c:\..\test\index.js:5:0: 'describe' is not defined.  
c:\..\test\index.js:6:2: 'describe' is not defined.  
c:\..\test\index.js:7:4: 'it' is not defined.
Run Code Online (Sandbox Code Playgroud)
什么是使标准不抱怨这些功能最干净的方法?
我现在正在使用mocha进行javascript单元测试.
我有几个测试文件,每个文件都有一个before和beforeEach,但它们完全相同.
我如何提供全局before及beforeEach所有这些(或其中一些)?