显式测试库/gems 是否由 rspec 中的存根定义?
我正在构建一个非常小的 gem 来读取图像数据格式并将图像作为无聊的 ruby 数组返回,或者返回一个漂亮的 narray,取决于 a) 是否已请求 narray 以及 b) narray gem 是否可用且需要。我的设计是,如果有人想使用 narray 他们可以,但他们必须事先明确要求它。读取图像数据时,我检查 narray 是否已定义(已被要求),并简单地向 msg 实例变量添加警告以供以后打印/记录。
我想对此进行一个测试(我使用 rspec2 和 mocha 进行模拟),但我对实际使用 rspec 有点陌生。我想做一些类似的事情:
it "should return an narray if requested" do
obj = NRead.new(@string, :bin => true, :narray => true)
obj.image_narray.should_not be_nil
end
it "should add an NArray Install message and not set the image_narray if NArray was not available" do
Object.stubs(:defined?).with(:NArray).returns(:false)
obj = NRead.new(@string, :bin => true, :narray => true)
obj.msg.should_not be_empty
obj.msg.grep(/Please gem install narray/).empty.should be_false
obj.image_narray.should be_nil
obj.image_ruby_array.size.should == @fixture_image_length
end
...但是我一定错过了使用存根的点,因为这个存根并没有改变定义的行为?无论存根如何,定义如何?返回“真实”值(即,如果我取消注释代码中的 require,则定义?为 false 并且我的警告已成功添加)。
这甚至不值得测试吗?有更好的方法吗?任何想法将不胜感激。谢谢,
I'm building a very small gem to read an image data format and return the image either as a boring ruby array, or a nice narray, depending on a) if an narray has been requested and b) if the narray gem is available and required. My design is that if someone wants to use narray they can, but they have to require it explicitly before-hand. When reading the image data, I check to see if narray is defined (has been required), and simply adds a warning to a msg instance variable for later printing/logging.
I'd like to include a test for this (I'm using rspec2 and mocha for mocking) but I'm a little new to actually using rspec. I'd like to do something like:
it "should return an narray if requested" do
obj = NRead.new(@string, :bin => true, :narray => true)
obj.image_narray.should_not be_nil
end
it "should add an NArray Install message and not set the image_narray if NArray was not available" do
Object.stubs(:defined?).with(:NArray).returns(:false)
obj = NRead.new(@string, :bin => true, :narray => true)
obj.msg.should_not be_empty
obj.msg.grep(/Please gem install narray/).empty.should be_false
obj.image_narray.should be_nil
obj.image_ruby_array.size.should == @fixture_image_length
end
...but I must be missing the point of using stubs, because this stubbing isn't changing the behavior of defined?. Regardless of the stub, defined? returns the "real" value (i.e. if I uncomment the require in the code, defined? is false and my warning is successfully added).
Is this not even worth testing? Is there a better way to do this? Any thoughts would be appreciated. Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
定义?不是对象的方法。这就是你的存根不起作用的原因。它是一个红宝石运算符。我不确定如何消除运算符。这绝对值得测试,因为它对代码的功能很重要。
要测试这一点,您可以从 ruby 中的 $" 数组中弹出 gem,该数组具有 kernel.load 信息并在 before 块中调用:
然后在 after 块中您可以重新请求它。这有点 hackish 也许有更好的方法。
defined? is not a method of Object. This is why your stub is not working. It is a ruby operator. I am not certain how to stub out an operator. This is definitely worth testing as it is important to the functioning of your code.
To test this you can pop the gem out of $" array in ruby which has the kernel.load info and call in before block:
Then in after block you can rerequire it. It is a bit hackish perhaps there is a better way.