为什么我无法从jasmine中的javascript函数访问全局变量

时间:2016-03-12 20:49:46

标签: javascript jasmine

我正在使用Jasmine独立https://github.com/jasmine/jasmine/releases

我在SpecRunner.html中声明了一个全局变量global_song(我可以从chrome控制台访问它,所以它真的是全局的),其中包含我试图将global_song连接到“应该能够播放Song”的脚本:< / p>

it("should be able to play Song " + global_song, function() {
player.play(song);
expect(player.currentlyPlayingSong).toEqual(global_song);

//demonstrates use of custom matcher
expect(player).toBePlaying(song);
});

为什么它无法访问global_song变量?

更新expect(player.currentlyPlayingSong).toEqual(global_song)有效,而it("should be able to play Song " + global_song无效。

2 个答案:

答案 0 :(得分:0)

你在哪里定义了global_song?如果你在beforeEach()函数中执行了这个操作,那么这个行为就有意义了,因为describe块(它试图定义你的it()函数)中的代码在before {(3}}其他SO中描述的beforeEach()之前执行。回答。

答案 1 :(得分:0)

我想假设你的 $paths = array(APP_LIBS.DIRECTORY_SEPARATOR.'Doctrine'.DIRECTORY_SEPARATOR.'Entity'); // the connection configuration $dbParams = array( 'dbname' => $di->get('config')->database->dbname, 'user' => $di->get('config')->database->username, 'password' => $di->get('config')->database->password, 'host' => $di->get('config')->database->host, 'port' => (int)$di->get('config')->database->port, 'charset' => 'UTF8', 'driver' => 'pdo_mysql' ); $config = Setup::createAnnotationMetadataConfiguration($paths, $di->get('env')->isDebugActive(), APP_PATH_CACHE_DOCTRINE, null, false); $config->setAutoGenerateProxyClasses($di->get('env')->isDev()); $driverChain = new Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain(); $cachedAnnotationReader = $config->getMetadataDriverImpl()->getReader(); \Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $cachedAnnotationReader); $driverChain->addDriver($config->getMetadataDriverImpl(), "Doctrine\Entity"); $config->setMetadataDriverImpl($driverChain); if ((bool)$di->get('config')->application->sqlLog) { $config->setSQLLogger(new \Doctrine\Logger\FileSQLLogger()); } $config->addCustomNumericFunction('RAND', 'Doctrine\Custom\Functions\Rand'); $config->addCustomStringFunction('REGEXP', 'Doctrine\Custom\Functions\Regexp'); $config->addCustomStringFunction('CONCAT_WS', 'Doctrine\Custom\Functions\ConcatWs'); // create event manager and hook preferred extension listeners $evm = new Doctrine\Common\EventManager(); // gedmo extension listeners, remove which are not used // sluggable $sluggableListener = new Gedmo\Sluggable\SluggableListener; // you should set the used annotation reader to listener, to avoid creating new one for mapping drivers $sluggableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($sluggableListener); // tree $treeListener = new Gedmo\Tree\TreeListener; $treeListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($treeListener); // loggable, not used in example $loggableListener = new Gedmo\Loggable\LoggableListener; $loggableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($loggableListener); // timestampable $timestampableListener = new Gedmo\Timestampable\TimestampableListener; $timestampableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($timestampableListener); // translatable $translatableListener = new Gedmo\Translatable\TranslatableListener; // current translation locale should be set from session or hook later into the listener // most important, before entity manager is flushed $translatableListener->setTranslatableLocale('en'); $translatableListener->setDefaultLocale('en'); $translatableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($translatableListener); // sortable, not used in example $sortableListener = new Gedmo\Sortable\SortableListener; $sortableListener->setAnnotationReader($cachedAnnotationReader); $evm->addEventSubscriber($sortableListener); // mysql set names UTF-8 if required $evm->addEventSubscriber(new Doctrine\DBAL\Event\Listeners\MysqlSessionInit()); $entityManager = EntityManager::create($dbParams, $config, $evm); $platform = $entityManager->getConnection()->getDatabasePlatform(); \Doctrine\DBAL\Types\Type::addType('json', '\Sonata\Doctrine\Types\JsonType'); $platform->registerDoctrineTypeMapping("json", 'json'); $platform->registerDoctrineTypeMapping('enum', 'string'); 在执行

后创建了
global_song

这种方式在测试中不可用作player.play(song); 的第一个参数,并且在it断言中执行player.play后可用。

尝试从expect(player.currentlyPlayingSong).toEqual(global_song)单独添加简单作业global_song,以验证它在执行player.play之前是否可用:

player.play

只需查看该示例,以说明问题的主要候选人:

window.global_song = 'value'