2015-09-20 54 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

哦,我爱上莎士比亚。谢谢!! –

相关问题