我試圖從電子書中運行一些示例RSpec
示例,但它看起來該書的舊版本爲RSpec
,因此一些示例引用了舊的RSpec API
,這是造成問題的原因。我試圖儘可能解決他們,但因爲我是Ruby
& RSpec
對我來說有點挑戰性。無法訪問RSpec測試中的變量
從錯誤日誌中我可以看出這是一個範圍問題,但不知道如何解決它。
是subject
仍然是rspec 3.4.2版本的一部分嗎?
$rspec --version
3.4.2
不起作用
require "spec_helper"
describe Location do
describe "#initialize" do
subject { Location.new(:latitude => 38.911268, :longitude => -77.444243) }
expect(:latitude).to eq(38.911268)
expect(:longitude).to eq(-77.444243)
end
end
錯誤日誌:
method_missing
:expect
不可上的示例基團(例如一個或describe
context
塊)。它只能在各個示例(例如it
塊)內或來自在示例範圍內運行的構造(例如before
,let
等)提供。 (RSpec :: Core :: ExampleGroup :: WrongScopeError)
錯誤消息告訴你到底發生了什麼問題:你在示例以外使用'except'。它必須在'it'塊中。附:他們仍然無法工作。符號':latitude'僅等於':latitude'(就像字符串''latitude'''只等於''latitude''')。它永遠不會等於一個數字。也許你的意思是'subject.latitude'? –