2015-09-20 86 views
1

我想測試我的布爾字符串的方法之一,但我得到了波紋管錯誤:Rspec的和Rails的:未定義的方法`到」真正:TrueClass

undefined method `to' for true:TrueClass 
describe 'is_tall?' do 

    it "should return true for a tall user" do 
    expect(tall_user_string.is_tall?.to be_truthy) 
    end 

    it "should return false for a short user" do 
    expect(user_string.is_tall?.to be_falsey) 
    end 

end 

任何想法?

回答

3

to調用應遵循expect(),而不是真正的方法。更改

describe 'is_tall?' do 

    it "should return true for a tall user" do 
    expect(tall_user_string.is_tall?.to be_truthy) 
    end 

    it "should return false for a short user" do 
    expect(user_string.is_tall?.to be_falsey) 
    end 

end 

describe 'is_tall?' do 

    it "should return true for a tall user" do 
    expect(tall_user_string.is_tall?).to be_truthy 
    end 

    it "should return false for a short user" do 
    expect(user_string.is_tall?).to be_falsey 
    end 

end 
0

你有一個小錯字,你需要提前關閉括號:

describe 'is_tall?' do 
    it "should return true for a tall user" do 
    expect(tall_user_string.is_tall?).to be_truthy 
    end 

    it "should return false for a short user" do 
    expect(user_string.is_tall?).to be_falsey 
    end 
end 

,如果你喜歡莎士比亞,你也可以寫這樣的:

describe 'is_tall?' do 
    it "should return true for a tall user" do 
    expect(tall_user_string.is_tall?).to be 
    end 

    it "should return false for a short user" do 
    expect(user_string.is_tall?).not_to be 
    end 
end 
+0

哦,我愛上莎士比亞。謝謝!! –

相關問題