jjzjj

javascript - fixture 不会在 Karma 中加载

coder 2024-05-13 原文

我已经尝试了很多方法,但在运行 Karma 时似乎无法加载固定装置。我试过使用 html2jskarma-jasmine-jquery —我倾向于后者,但无论哪个将我的固定装置加载到 DOM 中,我都会接受)—但据我所知,当我的测试运行时,它没有被加载并附加到 DOM。

我的目录结构很简单:

img/
  ↳ mac.png
  ↳ link.png
  ↳ pocketwatch.png
js/
  ↳ spriteloader.js
  ↳ spriteloader.spec.js
karma/
  ↳ imagelist.fix.html
  ↳ karma.conf.js
Gruntfile.coffee
index.html
grunt.config.coffee
package.json

这些是我安装的节点模块:

grunt 0.4.5
grunt-contrib-jshint 0.10.0
grunt-contrib-watch 0.6.1
grunt-karma 0.9.0
karma 0.12.23
karma-chrome-launcher 0.1.4
karma-firefox-launcher 0.1.3
karma-html2js-preprocessor": "^0.1.0",
karma-jasmine 0.2.0
karma-jasmine-jquery 0.1.1
karma-safari-launcher 0.1.1
karma-story-reporter 0.2.2

这是我的 karma.conf.js设置:

basePath: '../',

frameworks: ['jasmine-jquery', 'jasmine'],

files: [
  // Source and spec files
  {
    pattern: 'js/*.js',
    watched: true,
    served: true,
    included: true
  },
  // Fixtures
  {
    pattern: 'karma/*.fix.html',
    watched: false,
    served: true,
    included: false
  }
],

exclude: [
],

preprocessors: {
},

reporters: ['story'],

port: 9018,

colors: true,

logLevel: config.LOG_INFO,

autoWatch: true,

browsers: ['Chrome', 'Firefox', 'Safari'],

singleRun: true

在我的规范中,我从 describe() 开始用beforeEach()运行以下两行:

jasmine.getFixtures().fixturesPath = '../';
jasmine.getFixtures().load('/karma/imagelist.fix.html');

然后 it()功能启动。他们正在测试一个函数,该函数将事件监听器添加到某些 <img>元素使用 document.getElementById() .这就是 fixture 需要进来的地方,因为没有它 getElementById()将返回 null . (这是我遇到的问题。)

我在网上搜索了将近一整天,寻找可以尝试的东西,但除了 TypeError: document.getElementById(...) is null 之外,我似乎无法从 Karma 中得到任何东西。 .我已经尝试更改我的 Karama 配置,而不是空的,preprocessors"**/*.html": []在其中禁用html2js ,但那什么也没做。我试过禁用 jasmine-jquery并使用 html2js相反,但几乎没有关于 karma-html2js-preprocessor 的任何文档,所以我什至不知道我是否正确使用它。 (这就是为什么我更倾向于 jasmine-jquery 的原因。)我就是想不通。

更新(10 月 3 日)

我找到了 this question on Stack Overflow并在那里尝试了答案,但它没有用——我仍然遇到了我一直看到的相同行为。我的karma.conf.js与上面没有变化,但在我的spriteloader.spec.js我将 Jasmine 调用更改为:

jasmine.getFixtures().fixturesPath = 'http://localhost:9019/base/karma/';
jasmine.getFixtures().load('imagelist.fix.html');

我也试过了,还是一样的结果:

jasmine.getFixtures().fixturesPath = 'http://localhost:9019/base/karma/';
jasmine.getFixtures().load('http://localhost:9019/base/karma/imagelist.fix.html');

然后我找到了this issue thread on GitHub并更改了 preprocessorskarma.conf.js对此:

preprocessors: {
    '**/*.html': []
},

我将规范中的 Jasmine 调用更改为:

var fixtures = jasmine.getFixtures();
fixtures.fixturesPath = 'base/karma/';
fixtures.load('imagelist.fix.html');

这也导致了相同的行为:TypeError: document.getElementById(...) is null

我还找到了this post并尝试了其中概述的配置——除了添加 JASMINEJASMINE_ADAPTERfileskarma.conf.js —但我仍然有同样的行为。

因为我有karma-jasmine-jquery在本地安装,我指着 jasmine-jquery像这样的脚本:

'node_modules/karma-jasmine-jquery/lib/jasmine-jquery.js'

我什至尝试完全放弃 Jasmine 调用,转而使用 AJAX 调用:

$('#fixture').remove();
$.ajax({
    async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded
    dataType: 'html',
    url: '../karma/imagelist.fix.html',
    success: function(data) {
        $('body').append($(data));
    }
});

不过我还是得到了同样的结果。不确定我还能尝试什么。

更新(10 月 4 日)

我找到了 this question/answer on StackOverflow并尝试使用 html2js 进行设置根据那里的解决方案。我删除了 jasmine-jquery来自 frameworks我的部分 karma.conf.js , 并添加了一个 plugins看起来像这样的部分:

plugins: [
    'karma-chrome-launcher',
    'karma-firefox-launcher',
    'karma-html2js-preprocessor',
    'karma-jasmine',
    'karma-safari-launcher',
    'karma-story-reporter'
],

我也改变了我的 preprocessors部分看起来像这样:

preprocessors: {
    '**/*.html': ['html2js']
},

然后我更改了我的 beforeEach()到以下内容:

beforeEach(function() {
    var imagelist = __html__['../karma/imagelist.fix.html'];
    document.body.appendChild(imagelist);
});

我仍然看到相同的 TypeError: document.getElementById(...) is null , 不过。

最佳答案

如果您使用karma-jasmine-jquery,您只需设置以下内容:

  1. 将其添加为框架 - frameworks: ['jasmine-jquery', 'jasmine']

  2. 将您的灯具作为模式包含在文件数组下 - { pattern: 'test/fixtures/*.html', included: false, served: true }

  3. 在您的测试套件中使用 base/ 设置路径,例如 jasmine.getFixtures().fixturesPath = 'base/test/fixtures';

  4. 将 fixture 加载到您的测试规范中 - loadFixtures('index.html');

karma - Jasmine -jquery https://www.npmjs.com/package/karma-jasmine-jquery

karma 配置 http://karma-runner.github.io/0.12/config/files.html

更新 02.16.16

较新版本的 Chrome 不允许 file://URI 读取其他 file://URI。实际上,jasmine-jquery 无法在某些版本的 Chrome 下正确加载固定装置。对此的替代是使用开关 --allow-file-access-from-files 运行 Chrome。 https://github.com/velesin/jasmine-jquery#cross-domain-policy-problems-under-chrome

关于javascript - fixture 不会在 Karma 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26153241/

有关javascript - fixture 不会在 Karma 中加载的更多相关文章

  1. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  2. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  3. ruby-on-rails - 使用 javascript 更改数据方法不会更改 ajax 调用用户的什么方法? - 2

    我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的

  4. ruby-on-rails - prawnto 显示新页面时不会中断的表格 - 2

    我有可变数量的表格和可变数量的行,我想让它们一个接一个地显示,但如果表格不适合当前页面,请将其放在下一页,然后继续。我已将表格放入事务中,以便我可以回滚然后打印它(如果高度适合当前页面),但我如何获得表格高度?我现在有这段代码pdf.transactiondopdf.table@data,:font_size=>12,:border_style=>:grid,:horizontal_padding=>10,:vertical_padding=>3,:border_width=>2,:position=>:left,:row_colors=>["FFFFFF","DDDDDD"]pdf.

  5. ruby - 在 Mechanize 中使用 JavaScript 单击链接 - 2

    我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan

  6. ruby-on-rails - Ruby:如何在 fixture 中配置枚举? - 2

    给定这个类:classUser我想创建一个如下所示的fixture:testuser1:id:1username:sampermission::permission_staff我尝试了多种语法变体,但没有找到有效的方法。结果user.permission为nil或0。我知道enum是最近添加的。这可以做到吗? 最佳答案 根据enumdocs你可以像这样通过类引用可枚举的:User.permissions[:permission_staff]工厂只是ruby​​代码——所以他们应该能够以相同的方式访问值testuser1:id:1us

  7. javascript - jQuery 的 jquery-1.10.2.min.map 正在触发 404(未找到) - 2

    我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文

  8. ruby-on-rails - 我将 Rails3 与 tinymce 一起使用。如何呈现用户关闭浏览器javascript然后输入xss? - 2

    我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如

  9. ruby-on-rails - 使用 ApplicationController.renderer.render 从 Controller 外部渲染的 Rails 5 不会在自身上设置变量 - 2

    我正在使用Rails5ApplicationController.renderer.render方法从模型中进行渲染。我需要将一些变量传递给我的布局,这是我使用locals选项完成的;如果直接访问此变量,则该变量在布局中可用,但不能通过self访问。这是我设置渲染的方式html_string=ApplicationController.renderer.render(file:"/#{template_path}/base/show",:formats=>[:pdf,:html],locals:{:@routing_form=>self,:controller_name=>contro

  10. ruby - 使用 Selenium WebDriver 启用/禁用 javascript - 2

    出于某种原因,我必须为Firefox禁用javascript(手动,我们按照提到的步骤执行http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript)。使用Ruby的SeleniumWebDriver如何实现这一点? 最佳答案 是的,这是可能的。而是另一种方式。您首先需要查看链接Selenium::WebDriver::Firefox::Profile#[]=

随机推荐