简体   繁体   English

使用Rhino进行JavaScript单元测试

[英]JavaScript unit testing with Rhino

I hope someone out there might be able to help me out with a coding problem I am having. 我希望有人可以帮助我解决我遇到的编码问题。 I am currently working with server side JavaScript code that executed within a third party Java application (mirth connect) that executes the implemented JavaScript using the Rhino JavaScript compiler. 我目前正在使用服务器端JavaScript代码,该代码在第三方Java应用程序(mirth connect)中执行,该应用程序使用Rhino JavaScript编译器执行实现的JavaScript。 This is of of special interest since I sometimes have the need to interact with external Java packages. 这是特别有趣的,因为我有时需要与外部Java包交互。

I have been looking at ways of formalising our testing process on this code. 我一直在研究如何在此代码上正式化我们的测试过程。 With this in mind I have been looking at JavaScript unit testing frameworks such as QUnit and Jasmine. 考虑到这一点,我一直在研究JavaScript单元测试框架,如QUnit和Jasmine。 Unfortunately as I understand it, these frameworks are primarily aimed at testing JavaScript code running in a browser environment, but I need to test as if the code were being executed on a server. 不幸的是,据我所知,这些框架主要用于测试在浏览器环境中运行的JavaScript代码,但我需要测试代码是否在服务器上执行。 But this could be achieved by running these frameworks with a headless browser browser. 但这可以通过使用无头浏览器浏览器运行这些框架来实现。

What I'd really like to know is if this is at all possible? 我真的想知道的是,这是否可能? I have looked at many resources that claim to be able to implement this but have been unable to do this myself. 我已经查看了许多声称能够实现此功能的资源,但我自己无法做到这一点。 If it is indeed possible, does anyone have any links or simple walkthroughs I could follow? 如果确实有可能,是否有人有任何链接或简单的演练我可以遵循?

I'd be more than happy to provide examples of the JavaScript code I'd want to test, the Java class I'd want to call from the JavaScript and the unit tests once anyone can confirm I'm sure I'm not barking up the wrong tree with this as a possible solution. 我非常乐意提供我想要测试的JavaScript代码的示例,我希望从JavaScript调用的Java类和单元测试一旦有人确认我确定我不会吠叫以此作为可能的解决方案的错误树。

Thanks in advance! 提前致谢!

I'd recommend writing your tests in Groovy/Java and running them that way. 我建议用Groovy / Java编写测试并以这种方式运行它们。 It's not ideal, but Rhino provides a handy bridge to get at the JavaScript objects. 它并不理想,但是Rhino提供了一个方便的桥梁来获取JavaScript对象。

I've written about setting it up here: http://ryanbrooks.co.uk/posts/2014-03-27-testing-rhino-js-spock/ 我写过关于在这里设置它的文章: http//ryanbrooks.co.uk/posts/2014-03-27-testing-rhino-js-spock/

You could always try writing your tests in Jasmine and running them with Karma. 您可以尝试在Jasmine中编写测试并使用Karma运行它们。 You'd need to run Karma on Rhino, which caused us problems, but you might have more luck if you use CommonJS for Rhino. 你需要在Rhino上运行Karma,这会给我们带来麻烦,但如果你使用CommonJS for Rhino,你可能会有更多的运气。

My view is that (generally) Rhino is only used these days where JavaScript needs to be executed by Java processes, in which case you probably care about testing the Java-side output, but YMMV. 我的观点是(通常)Rhino现在仅用于需要Java进程执行JavaScript的情况,在这种情况下,您可能关心测试Java端输出,但是YMMV。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM