【问题标题】:any_instance should_receive not working correctlyany_instance should_receive 无法正常工作
【发布时间】:2012-05-31 15:26:21
【问题描述】:

我有这样的代码:

@dns = "#{params[:domain].split('/').reverse.join('.')}.#{params[:zone]}"
w = Whois::Client.new
@r = w.query(@dns)

有路线:

match "domains/:zone/*domain" => "domains#show"

我正在使用Whois Gem

而我要测试的是,所以我有一个测试:

it "should query 'google.com.ua' from /ua/com/google" do
    get :show, :zone => 'ua', :domain => 'com/google'
    dns = "google.com.ua"
    Whois::Client.any_instance.should_receive(:query).with(dns)
end

结果我得到:

Exactly one instance should have received the following message(s) but didn't: query

问题可能出在哪里?我 100% 确定 .query 被调用

【问题讨论】:

  • sscce.org。你的问题很短,但不是独立的;编写一个完整的脚本,以便人们可以运行它并重现问题。

标签: ruby-on-rails ruby rspec rspec-rails


【解决方案1】:

看起来您可能在调用查询方法后设置了期望值?

【讨论】:

  • 该死,我明白了,我的错。呵呵
  • should_receive 检查未来,而不是过去
  • @Uko:看看为什么发布一个完整的例子很重要 ;-)
  • 我也有同样的问题。我使用Model.any_instance.should_receive(:method) 好像是一种期望。 (有点像,但正如@ArtShayderov 所说,它检查的是未来,而不是过去)。
猜你喜欢
  • 1970-01-01
  • 2012-04-05
  • 2016-12-01
  • 1970-01-01
  • 2016-09-01
  • 2012-07-11
  • 2018-04-08
  • 2017-04-20
  • 2018-10-02
相关资源
最近更新 更多