ruby - Rspec 双返回 NoMethodError

标签 ruby testing rspec

不确定我的语法是否正确,它似乎无法识别我传递给替身的 stub 。

class Robot
  def boogie friend
    friend.dances
  end
end

测试:

describe Robot do

let(:robo_friend){double(:friend, {dances: true})}

  it "should have a friend dance too" do
    expect(subject.boogie :robo_friend).to be true
  end

end

错误:

Robot should have a friend dance too
 Failure/Error: expect(subject.boogie :robo_friend).to be true
 NoMethodError:
   undefined method `dances' for :robo_friend:Symbol
 # ./lib/robot.rb:3:in `boogie'
 # ./spec/robot_spec.rb:8:in `block (2 levels) in <top (required)>'

谁能看出我做错了什么?

最佳答案

这会起作用,您需要传递对象而不是符号。

describe Robot do
  let(:robo_friend) { double("Friend", dances: true) }

  it "should have a friend dance too" do
    expect(subject.boogie(robo_friend).to eq(true)
  end
end

关于ruby - Rspec 双返回 NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31636626/

相关文章:

ruby - clojure 中的屏幕抓取

ruby - 为什么让(:counter) { 0 } returns nil in example?

ruby - 我得到 "undefined method ljust";这个 Ruby 程序有什么问题?

带有基类和子类的 Python 单元测试

user-interface - 从 X11 GUI 中提取文本?

ruby - 重构 Ruby : Converting string array to int array

rspec - 如何让rspec加载config.ru?

ruby - Ruby 中的可靠 block ?

android - 使用 `?attr/` 时 ImageButton 的 Espresso InflateException

node.js - 如何在 Travis CI 上的一个项目中运行 Node.js 和 Ruby 测试