2013-08-06 61 views
2

我敢肯定這是一個令人難以置信的愚蠢問題,但我是Cucumber,Ruby和Rspec的新手,我正在爲一些非常基礎的事情而苦苦掙扎。在Rspec中使用期望匹配器

我安裝了RSpec的寶石,我想用期望的匹配: 預期(實際)。爲了EQ(預期)

好像我應該能夠僅僅需要rspec的/期望,然後執行這些命令,但它不起作用。

我錯過了什麼?

1.9.3-p448 :001 > require 'rspec' 
=> true 
1.9.3-p448 :002 > require 'rspec/expectations' 
=> true 
1.9.3-p448 :003 > expected = "this" 
=> "this" 
1.9.3-p448 :004 > actual = "this" 
=> "this" 
1.9.3-p448 :005 > expect(actual).to eq(expected) 
NoMethodError: undefined method `expect' for main:Object 
from (irb):5 
from /Users/lpc/.rvm/rubies/ruby-1.9.3-p448/bin/irb:16:in `<main>' 

奇怪的'應該'工作正常。

1.9.3-p448 :006 > expected.should == actual 
=> true 

感謝您的任何幫助。

回答

2

如果使用Cucumber,只需在env.rb文件中包含'rspec/expectations',然後在步驟定義中使用預期的語法。

env.rb:

require 'rspec/expectations' 

steps.rb:

Then(/^I should be able to use rspec$/) do 
    expected = "this" 
    actual= "this" 
    expect(actual).to eql(expected) 
end 

我的錯誤是在mistakingly紅寶石控制檯測試此語法。你不能這樣做,因爲黃瓜爲你做了一些automagic的東西。

「當黃瓜看到定義的Spec :: Matcher和Spec :: Expectations模塊時,它將隱式地將它們包含在執行這些步驟的World中。」

2

我真的很想能夠在Rails控制檯中使用expect()匹配器進行實驗。我意識到我可以將它們包裝在describe/it塊中,但是在控制檯中我沒有得到有用的結果。

describe "basic rspec feasure" do it "should do my bidding" do expect(:a).to eq(:a) end; end
返回RSpec::Core::ExampleGroup::Nested_2

沒有人能洞察一個如何能夠做這樣的事情?我很好奇發送通過/失敗條件的路徑是否在rspec內部。我知道這是rspec的非典型和高級用法,但我很好奇;)

+1

'test = describe「basic rspec feasure」do it「should do my bidding」do expect(:a).to eq(: a)結束;如果測試通過,結束''test.run'將是真的 – ivanxuu