Tag: rspec

RSpec如何打开?

我一直试图open ,开放的uri版本,我没有成功。 我已尝试执行以下操作,但请求仍在继续: Kernel.should_receive(:open).and_return(“Whatever for now”) 我也尝试过 OpenURI::OpenRead.should_receive(:open).and_return(“Whatever for now”) 自从我追踪到了在OpenURI中发出HTTP请求的地方。 在此先感谢您的任何建议!

如何在RSpec中测试信号处理,特别是处理SIGTERM?

Heroku可能出于各种原因向您的应用程序发送SIGTERM,因此我创建了一个处理程序来处理一些清理,以防万一。 一些谷歌搜索没有得到任何关于如何在RSpec中测试这个问题的答案或例子。 这是基本代码: Signal.trap(‘TERM’) do cleanup end def cleanup puts “doing some cleanup stuff” … exit end 测试程序收到SIGTERM时调用此清理方法的最佳方法是什么?

如何使用webmock正则表达式匹配器?

如何匹配以下url: http://www.example.com/foo/:id/bar http://www.example.com/foo/1/bar http://www.example.com/foo/999/bar stub_request(:post,“www.example.com”)

结构与测试在ruby中加倍

在结构上使用rspec double有什么优缺点? 例如 before :each do location = double “locatoin” location.stub(:id => 1) end VS before :each do location = Struct.new(“locatoin”, :id) location.new.id = 1 end

RSpec与Shoulda?

我是unit testing场的新手; 我现在只使用了大约2个月的unit testing。 当我在Ruby中进行unit testing时,我目前遵循TDD样式并使用Test :: Unit :: TestCase。 我还阅读了有关RSpec及其如何遵循BDD方法的内容。 我也读过关于Shoulda的内容,它介于两个框架之间。 我的问题是,我应该把时间花在RSpec或Shoulda上,或者我应该坚持使用Test :: Unit :: TestCase? 任何有关该主题的意见表示赞赏。

Capybara :: ElementNotFound:无法找到xpath“/ html”

我正在http://ruby.railstutorial.org/chapters/static-pages上关注Ruby on Rails教程并遇到以下错误 StaticPages Home page should have the content ‘Sample App’ Failure/Error: page.should have_content(‘Sample App’) Capybara::ElementNotFound: Unable to find xpath “/html” # (eval):2:in `text’ # ./spec/requests/static_pages_spec.rb:7:in `(root)’ 我的Gem文件如下 source ‘http://rubygems.org’ gem ‘rails’, ‘3.0.10’ gem ‘jruby-openssl’ gem ‘activerecord-jdbcsqlite3-adapter’ group :development, :test do gem ‘webrat’ gem ‘rspec-rails’, “>= 2.10.0” gem ‘capybara’, “>= 1.1.2” end 如何摆脱这个错误并通过rspec? 源文件 […]

从rspec中的帮助程序规范访问会话

我的ApplicationHelper中有一个方法可以检查我的购物篮中是否有任何商品 module ApplicationHelper def has_basket_items? basket = Basket.find(session[:basket_id]) basket ? !basket.basket_items.empty? : false end end 这是我的帮助规范,我必须测试这个: require ‘spec_helper’ describe ApplicationHelper do describe ‘has_basket_items?’ do describe ‘with no basket’ do it “should return false” do helper.has_basket_items?.should be_false end end end end 但是,当我运行测试时,我得到了 SystemStackError: stack level too deep /home/user/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/testing/test_process.rb:13: 从调试开始,我看到在@ request.session的ActionDispatch :: TestProcess中正在访问该会话 ,并且@request是nil。 当我从我的请求访问会话specs @request是ActionController :: […]

WebMock模拟失败的API(没有互联网,超时++)

我试图使用webmock模拟来自web api的意外行为,例如找不到服务器和超时。 最好的方法是什么? 我能想到的就是做这样的事情: stubbed_request = stub_request(:get, “#{host}/api/something.json”). with(:headers => {‘Accept’=>’*/*’, ‘Content-Type’=>’application/json’, ‘User-Agent’=>’Ruby’}). to_return(:status => [500, “Internal Server Error”]) 这应该适用于像404等,但如何测试超时 , 服务器未找到/脱机服务器 , 没有互联网连接 ?

如何在测试操作时将值放入flash中

我正在尝试测试需要存储在flash中的值的操作。 def my_action if flash[:something].nil? redirect_to root_path if flash[:something] return end # Do some other stuff end 在我的测试中,我做了类似的事情: before(:each) do flash[:something] = “bob” end it “should do whatever I had commented out above” do get :my_action # Assert something end 我遇到的问题是flash在my_action中没有值。 我猜这是因为没有请求实际发生。 有没有办法为这样的测试设置闪存?

如何在ruby中清除rspec测试之间的类变量

我有以下类:我想确保类url仅为所有实例设置一次。 class DataFactory @@url = nil def initialize() begin if @@url.nil? Rails.logger.debug “Setting url” @@url = MY_CONFIG[“my value”] end rescue Exception raise DataFactoryError, “Error!” end end end 我有两个测试: it “should log a message” do APP_CONFIG = {“my value” => “test”} Rails.stub(:logger).and_return(logger_mock) logger_mock.should_receive(:debug).with “Setting url” t = DataFactory.new t = nil end it “should throw an […]