摩卡+黄瓜嘲笑净响应 [英] Mocha + Cucumber to mock the Net response

查看:299
本文介绍了摩卡+黄瓜嘲笑净响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是应用程式/ models / websites.rb

The following is the app/models/websites.rb

class Masterpiece < ActiveRecord::Base
    validates_presence_of :title, :link
    validates_uri_existence_of :link, :allow_redirect => false
end

第二个验证来自插件验证URI 插件的存在

The second validation is from the plugin Validates Existence of URI plugin

以下是features / support / mocha.rb file

The following is the features/support/mocha.rb file

require 'mocha'
World(Mocha::API)

Before do
  mocha_setup
  @http_mock = mock('Net::HTTPResponse')
  @http_mock.stubs(:code => '200', :message => "OK", :content_type => "text/html", :body => '<title>Test</title><body>Body of the page</body>')
  Net::HTTP.expects(:get_response).returns(@http_mock)
  #Website.expects(:validates_uri_existence_of).returns(true)
end

After do
  begin
    mocha_verify
  ensure
    mocha_teardown
  end
end


b $ b

但是当我尝试运行一个黄瓜功能,它会尝试创建记录,在保存上面的插件将尝试检查网络以获得响应。它的罚款。

但是当我想要在测试环境中模拟时,我正在尝试使用mocha。

But when I try to run a cucumber feature, it will try to create the record and before saving the above plugin will try to check over the Net to get the response. Its fine.
But when I want to get it Mocked on test environment, I'm trying to use mocha.

如何写模拟Net响应或类方法的代码 >

How shall I write the code to mock the Net response or the class method validates_uri_existence_of to run the test smoothly??

推荐答案

我建议使用 Fakeweb ,因为它是完美的。

I'd recommend using Fakeweb for this, as it is perfect for it.

这篇关于摩卡+黄瓜嘲笑净响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆