开始使用黄瓜的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')
Run Code Online (Sandbox Code Playgroud)
非常感谢!!
对于您正在进行的字符串比较==
,eq
并且(be .)
基本相同.
的match
是模式匹配和匹配的谐音,因此将匹配bRadiosity这会不会是其他方法一样,如果这是在整个文本a
锚标记
例如
1.9.3-p194 :001 > a="text with radio"
=> "text with radio"
1.9.3-p194 :002 > a.=='radio'
=> false
Run Code Online (Sandbox Code Playgroud)
和
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">
Run Code Online (Sandbox Code Playgroud)
注意:
== is ruby (which also has .eql? available though not shown here).
.eq is an rspec helper as is the (be .) construct
Run Code Online (Sandbox Code Playgroud)
就个人而言,我==
最喜欢字符串比较.其他人更喜欢,.eql
因为它更多地与=
(更突出,更少混乱)不同.我可能会喜欢==
更多,因为它的语言可以更容易移植.
归档时间: |
|
查看次数: |
2224 次 |
最近记录: |