Rspec& Rails:未定义的方法`to'for true:TrueClass

时间:2015-09-20 20:37:51

标签: ruby-on-rails rspec

我正在尝试测试我的一个布尔字符串方法,但是我收到了以下错误:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:1)

你有一个小错字,你需要先关闭括号:

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