开始对 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/