una tabla resueltos recta punto partir ordenadas los las intersecciones interseccion grafica encontrar ejes ejercicios coordenados con abscisas javascript unit-testing gruntjs mocha yeoman

javascript - tabla - ReferenceError: no se puede encontrar la variable x cuando se usa la prueba de ronco en yeoman



intersecciones de la recta con los ejes coordenados (3)

Probablemente llamas

<script>mocha.run()</script>

cuando DOM no está listo, intente esto

<script> if( document.readyState === "complete" ) { var creature = new Creature(''test''); alert(creature .name); //mocha.run() } </script>

o con jQuery

$(function() { var creature = new Creature(''test''); alert(creature .name); //mocha.run() });

Estoy usando yeoman, gruñido, mocha. Quiero hacer BDD, así que realizo la prueba básica de unidad y ejecuto la grunt test en la consola, lo que me da ReferenceError: can''t find variable: Creature / index.html en el navegador funciona como debería.

Este es mi archivo creature.js en la aplicación / scripts:

''use strict''; var Creature = (function () { function Creature(name) { this.name = name; } Creature.prototype.sayHello = function (message) { return this.name + '' '' + message; }; Creature.prototype.eat = function (item){ return this.name + '' is eating '' + item; } return Creature; })();

Esta es mi prueba / index.html

<!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Mocha Spec Runner</title> <link rel="stylesheet" href="bower_components/mocha/mocha.css"> </head> <body> <div id="mocha"></div> <script src="bower_components/mocha/mocha.js"></script> <script>mocha.setup(''bdd'')</script> <script src="bower_components/chai/chai.js"></script> <script> var assert = chai.assert; var expect = chai.expect; var should = chai.should(); </script> <!-- include source files here... --> <script src="../app/scripts/creature.js"></script> <!-- include spec files here... --> <script src="spec/test.js"></script> <script>mocha.run()</script> </body> </html>

Este es mi archivo test / spec / test.js:

/* global describe, it */ (function () { ''use strict''; describe(''Creature'', function () { describe(''Comunnications'', function () { it(''should say <name> hi'', function () { var creature = new Creature(''test''); expect(creature.sayHello(''hi'')).to.equal(''test hi''); }); it(''should say <name> is eating <item>'', function () { var creature = new Creature(''test''); expect(creature.eat(''pear'')).to.equal(''test is eating pear''); }); }); }); })();

Registro de consola:

Running "copy:styles" (copy) task Done, without errors. Running "autoprefixer:dist" (autoprefixer) task Running "connect:test" (connect) task Started connect web server on http://localhost:9001 Running "mocha:all" (mocha) task Testing: http://localhost:9001/index.html .. 0 passing (113ms) 2 failing 1) Creature Comunnications should say <name> hi: ReferenceError: Can''t find variable: Creature at http://localhost:9001/spec/test.js:9 at http://localhost:9001/bower_components/mocha/mocha.js:4263 at http://localhost:9001/bower_components/mocha/mocha.js:4635 at http://localhost:9001/bower_components/mocha/mocha.js:4694 at next (http://localhost:9001/bower_components/mocha/mocha.js:4561) at http://localhost:9001/bower_components/mocha/mocha.js:4570 at next (http://localhost:9001/bower_components/mocha/mocha.js:4514) at http://localhost:9001/bower_components/mocha/mocha.js:4538 at timeslice (http://localhost:9001/bower_components/mocha/mocha.js:5531) 2) Creature Comunnications should say <name> is eating <item>: ReferenceError: Can''t find variable: Creature at http://localhost:9001/spec/test.js:13 at http://localhost:9001/bower_components/mocha/mocha.js:4263 at http://localhost:9001/bower_components/mocha/mocha.js:4635 at http://localhost:9001/bower_components/mocha/mocha.js:4694 at next (http://localhost:9001/bower_components/mocha/mocha.js:4561) at http://localhost:9001/bower_components/mocha/mocha.js:4570 at next (http://localhost:9001/bower_components/mocha/mocha.js:4514) at http://localhost:9001/bower_components/mocha/mocha.js:4538 at timeslice (http://localhost:9001/bower_components/mocha/mocha.js:5531) >> 2/2 tests failed (0.11s) Warning: Task "mocha:all" failed. Use --force to continue. Aborted due to warnings. Execution Time (2014-06-08 14:40:23 UTC) concurrent:test 3s ■■■■■■■■■■■■■■■ 32% connect:test 447ms ■■■ 5% mocha:all 5.9s ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 63% Total 9.4s

Tal vez tiene que ver con la referencia a ../app/scripts/creature.js cuando se ejecuta a través de phantomjs tiene una ruta diferente en comparación con su uso en un navegador real?

EDITAR

Parece que la grunt test abre el camino equivocado para probar. Parece abrir root / index.html Cuando se abre ... Lo tengo que trabajar cambiando la ruta a creature.js (eliminé la aplicación / )

<!-- include source files here... --> <script src="../scripts/creature.js"></script>

Sin embargo, cuando se ejecuta grunt test ahora funciona, pero al abrir el archivo test / index.html ahora me da el error ReferenceError: Creature is not defined en la consola de Firebug.

¿Hago algo mal o es ese comportamiento correcto? ¿Hay algo que pueda hacer para que funcione en ambos sentidos?

Descubrí dónde estaba el problema cambiando la prueba modificada de gruntfile.js: options: open to true.

// The actual grunt server settings connect: { options: { port: 9000, open: true, livereload: 35729, // Change this to ''0.0.0.0'' to access the server from outside hostname: ''localhost'' }, livereload: { options: { ... } }, test: { options: { open: false, port: 9001, middleware: function(connect) { return [ connect.static(''.tmp''), connect.static(''test''), connect().use(''/bower_components'', connect.static(''./bower_components'')), connect.static(config.app) ]; } } }, dist: { ... } } }

¡Gracias por adelantado!

Posiblemente relacionado con la ejecución de Mocha en la línea de comando e Incluir un archivo


Has intentado poner

<script src="../app/scripts/creature.js"></script>

antes de

<script src="bower_components/mocha/mocha.js"></script>

Parece que la var no está definida y probablemente lo que Volkinc estaba buscando con su respuesta.


Esto es probablemente debido a esta línea. connect.static (config.app). Al ejecutarlo en la línea de comandos, se ejecuta connect y establece la ruta raíz / base (donde lee el archivo) a la aplicación y la prueba. Por lo tanto, si quita la aplicación, funciona para la línea de comandos, sin embargo, si la está ejecutando como un archivo, es relativa, por lo tanto, necesita una ''aplicación''.