简体   繁体   English

当你在嵌入式JRuby中执行Ruby脚本时,你能使用gems吗?

[英]Can you use gems, when you execute a Ruby script in embedded JRuby?

It so happened that I need to unit-test this Ruby script . 碰巧我需要对这个Ruby脚本进行单元测试。 It's a Sinatra -based web application that uses several gems. 这是一个基于Sinatra的Web应用程序,它使用了几个宝石。

I have more experience in Java than in Ruby, time is very important (and elegance isn't), and so I thought I may be faster, if I execute that script in a JUnit class and mock all subsystems it depends on (web server, database). 我在Java方面比在Ruby方面有更多的经验,时间非常重要(优雅不是),所以我认为我可能会更快,如果我在JUnit类中执行该脚本并模拟它所依赖的所有子系统(Web服务器) ,数据库)。 That way I could use my Java skills to test a Ruby system. 这样我就可以使用我的Java技能来测试Ruby系统。 It looks like you can embed Ruby in a Java program. 看起来您可以将Ruby嵌入Java程序中。

Question: Can I use gems in embedded Ruby, ie create a Java program that could execute the tests on any machine (Windows, Linux, Mac) and wouldn't require that I install several gems on it manually? 问题:我可以在嵌入式Ruby中使用gems,即创建一个可以在任何机器(Windows,Linux,Mac)上执行测试的Java程序,并且不需要我手动在其上安装几个宝石吗? If yes, how? 如果有,怎么样?

Note: They need to be real gems, mocks are fine. 注意:它们需要是真正的宝石,嘲笑都很好。

Update 1 (25.01.2017 13:14): One obvious solution is to extract the logic of that script into a separate Ruby file without dependencies. 更新1(25.01.2017 13:14):一个明显的解决方案是将该脚本的逻辑提取到一个没有依赖项的单独的Ruby文件中。 That probably would be 80 % of the code. 这可能是代码的80%。 Then I could test that logic file even without having the gems. 然后,即使没有宝石,我也可以测试该逻辑文件。

I am assuming you are using JRuby. 我假设你正在使用JRuby。 Check this and this out. 检查这个这个 With this maybe you can do the opposite and use Junit from within Jruby itself. 有了这个,你可以做相反的事情并在Jruby内部使用Junit。

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

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