Rspec 比较 : difference between should eq, 匹配,是,==?

标签 rspec

开始对 cucumber 使用 rspec 断言,我对用哪种方式进行字符串比较产生了疑问。我已经尝试了以下 4 种方法,它们似乎都产生了相同的结果,所以我想知道其中一种方法是否比其他方法更好?

而且,这 4 种方法的区别容易解释吗?也许举个例子?

page.first('div#navigation a').text.should == 'Radio')
page.first('div#navigation a').text.should eq('Radio')
page.first('div#navigation a').text.should match('Radio')
page.first('div#navigation a').text.should (be 'Radio')

非常感谢!!

最佳答案

对于您正在进行的字符串比较,== , eq(be .)基本相同。
match是模式匹配并且将匹配部分,因此将匹配 bRadiosity,如果那是 a 中的整个文本,则其他方法将不正确。 anchor 标签

例如

1.9.3-p194 :001 > a="text with radio"
 => "text with radio" 
1.9.3-p194 :002 > a.=='radio'
 => false 


1.9.3-p194 :013 > b="radioz"
 => "radioz" 
1.9.3-p194 :014 > b.=="radio"
 => false 
1.9.3-p194 :015 > b.match "radio"
 => #<MatchData "radio"> 

笔记:
== is ruby (which also has .eql? available though not shown here).
.eq is an rspec helper as is the (be .) construct

个人比较喜欢==最适合字符串比较。其他人更喜欢 .eql因为它与 = 的区别更大(更突出,更少困惑)。我可能喜欢 ==更多,因为它看起来更易于跨语言移植。

关于Rspec 比较 : difference between should eq, 匹配,是,==?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13893400/

相关文章:

ruby - 是否可以使用 Rspec 为无限循环问题编写规范?,Ruby

ruby-on-rails - 使用登录用户测试 Rails 功能(未定义方法 'session')

ruby-on-rails - 如何测试 RSpec in Rails 中的 204 响应?

ruby-on-rails-3 - rspec Controller 空 session

ruby - 在命令行上将参数传递给 Rspec 测试

ruby-on-rails - RSpec:如何期望两个不同对象上的有序消息?

ruby-on-rails - RSpec 对页面上的元素抛出误报

ruby-on-rails - Rspec 跳过 Controller 中的设计操作 - sign_in_and_redirect

ruby - RSpec 模拟失败 "undefined method ` -' for nil:NilClass"

ruby-on-rails - 测试环境中不调用 PostgreSQL 触发器(运行 RSpec 时)