【问题标题】:setting rspec expectations on internal java method calls from jruby对来自 jruby 的内部 java 方法调用设置 rspec 期望
【发布时间】:2010-07-04 13:53:29
【问题描述】:

我希望能够在 jruby 下使用 rspec 测试 java 代码,但看不到如何设置对内部 java 方法调用的期望。给定以下java:

public class A {
  public String hi() {
    return hello();
  }

  public String hello() {
    return "yo";
  }
}

我希望能够做到:

describe 'A' do
  it 'should call hello' do 
    a = some.java.package.A.new
    a.should_receive(:hello).and_return('yello')
    a.hi
  end
end

是否可以在后台集成一个 java 模拟工具来做到这一点?有人已经这样做了吗?我不在乎是否必须使用不同的语法来设置期望值(而不是 rspec 的“should_receive”),但它至少应该简洁。

【问题讨论】:

    标签: java mocking rspec jruby jmock


    【解决方案1】:

    JtestR 完全符合您的要求。它是与 JRuby 集成捆绑在一起的 Ruby 库集合,因此运行测试设置起来完全轻松。

    它捆绑了 Mocha 和 RSpec http://jtestr.codehaus.org/Mocks。 例如 Rspec for Map,你可以这样写模拟:

    it "should be able to add an entry to it" do
        @hash_map.put "foo", "bar"
        @hash_map.get("foo").should == "bar"
      end
    

    More info here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多