我已经设置了一个maven项目来使用jasmin-maven-plugin来测试我的javascript代码。该插件设置如下:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.2.0.0</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>src/main/webapp/js</jsSrcDir>
<jsTestSrcDir>src/test/javascript</jsTestSrcDir>
<sourceIncludes>
<include>jquery-1.8.2.min.js</include>
<include>jquery-ui-1.8.13.custom.min.js</include>
<include>jquery.json-2.2.js</include>
<include>stylesheet.js</include>
<include>**/*.js</include>
</sourceIncludes>
</configuration>
</plugin>
我编写了一个javascript方法,它使用jQuery进行事件处理。代码是这样的:
registerDragSurface: function ( element, onDrag ) {
// Wrap every drag surface registration in a closure
// to preserve the given set of arguments as local variables
(function($, element, dragdropsystem, onDrag) {
// Register a mousemove event on the drag surface
$(element).mousemove (function ( event ) {
// If the user is dragging
if (dragdropsystem.isDragging) {
// Call the onDrag method with
// the given element and event
onDrag (element, event);
}
});
// Register a mouseup event on the drag surface
$(element).mouseup (function ( event ) {
// If the user is dragging
if (dragdropsystem.isDragging) {
// Call the onDragEnded function
dragdropsystem.onDragEnded();
}
// We are not dragging anymore, and
// the left mousebutton has been released
dragdropsystem.isDragging = false;
dragdropsystem.isMouseDown = false;
});
})($, element, this, onDrag);
},
然后我写了一个茉莉花规格来测试这个功能。代码在这里:
describe("linkedforms.dragdrop", function() {
var dragdrop = linkedforms.dragdrop;
it("test that a drag surface can be registered and onDragCallback is called", function () {
var dragSurface = {};
var onDragCallback = jasmine.createSpy("onDragCallback");
dragdrop.registerDragSurface(dragSurface, onDragCallback);
dragdrop.isDragging = true;
jQuery(dragSurface).trigger("mousemove");
expect(onDragCallback).toHaveBeenCalled();
expect(dragdrop.isDragging).toBe(true);
jQuery(dragSurface).trigger("mouseup");
expect(dragdrop.isDragging).toBe(false);
});
});
我可以从命令行运行mvn jasmine:bdd,然后访问
http://localhost:8234
运行测试。这很好,我的所有规格都通过了。然后我尝试使用mvn测试从maven运行测试,但随后它中断了。它说:
* Expected spy onDragCallback to have been called.
* Passed.
* Expected true to be false.
这让我怀疑当从mvn test运行时,jQuery事件系统无法正常工作,因此在HtmlUnit浏览器中运行。有人知道如何解决这个问题吗?
答案 0 :(得分:0)
我发现如果我将jQuery实现从jquery-1.8.2.min.js [production]更改为jquery-1.8.2.js [development],它就可以工作。
任何人都有任何想法为什么会这样?