我正在使用jasmine-rails gem运行Rails 3.2.8。下面是我的Gemfile中描述jasmine设置的代码行:
jasmine (1.2.1)
jasmine-core (>= 1.2.0)
rack (~> 1.0)
rspec (>= 1.3.1)
selenium-webdriver (>= 0.1.3)
jasmine-core (1.2.0)
jasmine-headless-webkit (0.8.4)
coffee-script
jasmine-core (~> 1.1)
multi_json
rainbow
sprockets (~> 2)
jasmine-rails (0.1.0)
jasmine
jasmine-headless-webkit
rails (>= 3.1.0)我也在使用jasmine-jquery扩展。这组函数提供了我在测试中使用的toHaveText方法。我已经将jasmine-jquery扩展文件保存到spec/javascripts/helpers目录中。
直接位于spec/javascripts目录中名为UserInvitationsSpec.js的测试文件包含以下内容:
describe ("my basic jasmine jquery test", function(){
beforeEach(function(){
$('body').append('<a id="test_link" href="somewhere.html">My test link</a>');
});
afterEach(function(){
$('a#test_link').remove();
});
it ("does some basic jQuery thing", function () {
$('a#test_link').click();
expect($("a#test_link")).toHaveText('My test link is now longer');
});
it ("does some the same basic jQuery thing with a different trigger type", function () {
$('a#test_link').trigger('click');
expect($("a#test_link")).toHaveText('My test link is now longer');
});
});
describe ('subtraction', function(){
var a = 1;
var b = 2;
it("returns the correct answer", function(){
expect(subtraction(a,b)).toBe(-1);
});
});正如您所看到的,我已经包含了一个基本的javascript测试,以显示非jQuery测试正在按预期工作。
我的javascript文件core.js位于app/assets/javascripts目录中,其内容如下:
function subtraction(a,b){
return a - b;
}
jQuery (function($) {
$("a#test_link").click(changeTheTextOfTheLink)
function changeTheTextOfTheLink(e) {
e.preventDefault()
$("a#test_link").append(' is now longer');
}
});当我通过终端运行'bundle exec jasmine-headless-webkit‘时,输出如下:
FF.
FAIL: 3 tests, 2 failures, 0.013 secs.
my basic jasmine jquery test does some basic jQuery thing. (/Users/rebekah/OPSWAT/opswat_cwm/spec/javascripts/UserInvitationsSpec.js:11)
Expected '<a id="test_link" href="somewhere.html">My test link</a>' to have text 'My test link is now longer'. (line ~13)
expect($("a#test_link")).toHaveText('My test link is now longer');
my basic jasmine jquery test does some the same basic jQuery thing with a different trigger type. (/Users/rebekah/OPSWAT/opswat_cwm/spec/javascripts/UserInvitationsSpec.js:16)
Expected '<a id="test_link" href="somewhere.html">My test link</a>' to have text 'My test link is now longer'. (line ~18)
expect($("a#test_link")).toHaveText('My test link is now longer');我的application.js文件位于相同的app/assets/javascripts目录中,其中包含以下内容:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require vendor
//= require_tree . 我的jasmine.yml文件位于spec/javascript/support目录中,其中包含以下内容:
src_files:
- "application.{js,coffee}"
stylesheets:
helpers:
- "helpers/**/*.{js,coffee}"
spec_files:
- "**/*[Ss]pec.{js,coffee}"
src_dir: "app/assets/javascripts"
spec_dir: spec/javascripts
asset_paths:
- "vendor/assets/javascripts"你知道为什么在应用程序中正常工作的jQuery函数不能在测试环境中执行吗?
非常感谢!
-Rebekah
发布于 2012-09-11 04:24:35
jQuery的document.ready事件(jQuery(function ($) { … });)在Jasmine的beforeEach之前运行,这意味着您正在尝试将单击处理程序绑定到尚未添加到文档中的链接。
下面是一个简化的例子:
// application.js
jQuery(function ($) {
console.log("In document.ready");
});
// test_spec.js
describe("the order of things", function () {
beforeEach(function () {
console.log("In beforeEach");
});
it("should run some tests", function () {
console.log("In test");
});
});这将给出以下输出:
$ bundle exec jasmine-headless-webkit
"In document.ready"
Running Jasmine specs...
"In before each"
"In test"
.
PASS: 1 test, 0 failures, 0.004 secs.不幸的是,我在Jasmine Headless Webkit (这是jasmine-rails在幕后使用的)文档中找不到任何关于使用定制HTML模板的内容。
https://stackoverflow.com/questions/12358051
复制相似问题