ruby - 如何用 RSpec 测试这个?

标签 ruby rspec

我想使用 RSpec 测试某个方法是否在方法内被调用。采用以下示例类:

class Upload < ActiveRecord::Base
    def process
      case self.file_name
      when /\.html$/
        to_pdf
      end
    end
end

当在 file_name = 'foo.html' 的上传实例上调用 process 时,我如何测试 to_pdf 被调用?如果可能的话,我想使用测试替身来做到这一点。

最佳答案

instance = Upload.new
instance.file_name = "foo.html"

instance.should_receive(:to_pdf)

instance.process

关于ruby - 如何用 RSpec 测试这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6748481/

相关文章:

html - 如何在 HTML 标签中包装 Ruby 字符串

javascript - 将 JavaScript 转换为 Coffee?

sql - 将 UNION 与 Sequel 结合使用

ruby-on-rails - Rails -> Devise -> 如何从模块访问 current_user

ruby-on-rails - 在现有路由的 rspec 测试中找不到路由

ruby - 是接收(:method). 从不完全等同于 not_to 接收(:方法)

ruby-on-rails - 语法错误,意外 '\n' ,期望 => (SyntaxError)

ruby - sinatra,rack auth basic 和从文件中查找

ruby-on-rails - 测试使用 ActiveRecord 模型的 gem

ruby-on-rails-4 - 没有路由匹配 { :action= >"show" in RSpec, 在浏览器中正常