开发者

Using rspec to test code prone to external infuence

开发者 https://www.devze.com 2022-12-21 19:11 出处:网络
I\'m using rspec to test a code that may fail depending on the change of a site structure (the external influence I mentioned). I would like to write an example that involves \"should raise an error\"

I'm using rspec to test a code that may fail depending on the change of a site structure (the external influence I mentioned). I would like to write an example that involves "should raise an error" but开发者_开发知识库 I'm not sure if rspec is the right tool to test code in such situations. Could someone point me in some direction?

Thanks in advance


You could write custom matchers

Something like :

site.should_have_valid_structure

Spec::Matchers.define :have_structure
  match do |actual|
    actual.structure == Site::VALID_STRUCTURE
  end
end


Mock the external influence so you can test it properly (if the external influence is a Web page or other HTTP request, WebMock and VCR are great for this). Your tests should not rely on anything external functioning properly -- or improperly. See http://marnen.github.com/webmock-presentation/webmock.html for an overview I wrote last year.

0

精彩评论

暂无评论...
验证码 换一张
取 消