Tag: rspec

Rails端口测试环境

我想使用Faraday和RSpec测试我们的Rails应用程序的HTTP API。 法拉第需要主机url+端口。 不幸的是,测试环境的端口总是会改变。 如何在规范中以编程方式访问当前端口?

当试图运行rspec时,我得到“未初始化的常量ActiveModel”

当我运行rspec spec我得到以下内容: /usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions/active_record/base.rb:26:in`’:ininitialized constant ActiveModel(NameError ) 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions.rb:1:in`requiren’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails/extensions.rb:1:in` 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails.rb:8:in,requirement 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-rails-2.7.0/lib/rspec/rails.rb:8:in` 来自/Users/noahc/Dropbox/perfect_setup/spec/spec_helper.rb:4:in`requirement’ 来自/Users/noahc/Dropbox/perfect_setup/spec/spec_helper.rb:4:in`’ 来自/Users/noahc/Dropbox/perfect_setup/spec/controllers/pages_controller_spec.rb:1:in`requirent’ 来自/Users/noahc/Dropbox/perfect_setup/spec/controllers/pages_controller_spec.rb:1:in` 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in”load’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in”load_spec_files中的块’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in`map’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/configuration.rb:459:in,load_spec_files’ 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/command_line.rb:18:in“run” 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:80:in“run_in_process” 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:69:in“run” 来自/usr/local/lib/ruby/gems/1.9.1/gems/rspec-core-2.7.1/lib/rspec/core/runner.rb:10:in,clock in autorun’ 我的spec_helper.rb看起来像这样: ENV[“RAILS_ENV”] ||= ‘test’ require ‘spec_helper’ require ‘rspec/rails’ require ‘rspec/autorun’ require ‘spork’ Spork.prefork do ENV[‘RAILS_ENV’] ||= ‘test’ require File.expand_path(‘../../config/environment’, __FILE__) require ‘rspec/rails’ RSpec.configure do |config| config.mock_with :rspec config.fixture_path = “#{Rails.root}/spec/fixtures” config.use_transactional_fixtures […]

在尝试模拟按时,Capybara:NotSupportedByDriverError

我正在使用Capybara编写一个规范来测试我网站上搜索栏的function。 按照本页上有关如何模拟按Rspec / Capybara中的Enter键的说明操作后,运行测试时出现以下错误: Failure/Error: page.driver.execute_script(keypress) Capybara::NotSupportedByDriverError: Capybara::Driver::Base#execute_script 难道我做错了什么? 以下是我的spec文件的内容: require ‘spec_helper’ describe ‘Search’ do it ‘displays no results when non-existent things are looked up’ do visit root_path page.first(“.search-icon-small”).click fill_in “search”, with: “NonExistent” #simulate pressing Enter keypress =”var e = $.Event(‘keydown’, { keyCode: 13 }); $(‘body’).trigger(e);” page.driver.execute_script(keypress) page.should have_content(‘No Results’) end it ‘displays content that […]

黄瓜,rspec和rails 3引发错误选项: – profile

我第一次使用带有导轨3的黄瓜。 的Gemfile group :test do gem “rspec” gem “rspec-rails” gem “database_cleaner” gem “spork” gem “cucumber”, :git => “git://github.com/aslakhellesoy/cucumber.git” gem “cucumber-rails”, :git => “git://github.com/aslakhellesoy/cucumber-rails.git” gem “capybara” gem “capybara-envjs” gem “launchy” gem “ruby-debug” end 并用发电机安装黄瓜骨架 rails generate cucumber:install –rspec –capybara 这会生成一个cucumber.yml default: features wip: –tags @wip:3 –wip features rerun: –format rerun –out rerun.txt –strict –tags ~@wip 如果我运行它运行的最简单的function,但在测试后它会引发exception […]

正确使用shared_examples_for的方法

我可能对shared_examples_for应该做什么有一个错误的理解,但是听我说。 基本上,我有一个公共导航栏出现在index页面和论坛的new页面。 所以我希望测试导航栏能够同时执行index页面和new页面。 我希望下面的代码使用shared_examples_for来实现这一点。 但是发生的事情是, shared_examples_for中的shared_examples_for根本没有运行。 要检查我在shared_examples_for范围内创建了失败的测试用例,但测试没有失败。 我究竟做错了什么? require ‘spec_helper’ describe “Forums” do subject { page } shared_examples_for “all forum pages” do describe “should have navigation header” do it { should have_selector(‘nav ul li a’, text:’Home’) } it { should have_selector(‘nav ul li a’, text:’About’) } end end describe “Index forum page” do before { […]

如何使用RSpec测试ajax POST请求?

我只是想在控制器规范上测试ajax请求。 产品代码如下。 我正在使用Devise进行身份validation。 class NotesController < ApplicationController def create if request.xhr? @note = Note.new(params[:note]) if @note.save render json: { notice: "success" } end end end end 规格如下。 describe NotesController do before do user = FactoryGirl.create(:user) user.confirm! sign_in user end it “has a 200 status code” do xhr :post, :create, note: { title: “foo”, body: “bar” […]

在集成测试中使用命名路由是否合适?

其中哪一个是编写集成(请求)测试的“正确”方法。 it “should be successful” do get “/about/terms” response.should be_success end it “should be successful” do get about_terms_path response.should be_success end

检查Capybara的复选框

使用Capybara我不能为我的生活选择我的表格上的复选框。 在我的请求规范中,我尝试过: check(“First Name”) page.check(“First Name”) page.check(“pickem_option_ids_10”) find(:css, “#pickem_option_ids_11[value=’11’]”).set(true) find(:css, “#pickem_option_ids_11”).set(true) 我的表格片段: Options: First Name Middle Name 我从这个SO线程中得到了一些find()想法。 我在其他规格中取得了一些成功,我有一个标签为Active的复选框,我只是说check(“Active”) 。

RSpec和数据库清理程序 – 将某些对象永久保留在测试数据库中

我(姗姗来迟)开始使用RSpec / capybara测试我的Rails应用程序(购物网站),使用数据库清理程序清除数据库,并使用Factory Girl为每个测试生成新对象(像大多数人一样)。 这很好用,我认为清除测试之间的数据是个好主意。 但是,它可能会变慢并且(据我所知)生成同一对象的多个实例有点乏味。 在我的数据库中有一些对象总是相同的,或者我将始终为测试生成相同的副本。 例如,我的Package模型,它定义订阅包的定价和function限制。 它可能永远不会改变。 有没有办法,使用此配置(请注释并指定是否需要更多信息),将某些对象实例放入测试数据库并将其从数据库清理器中排除 ,或以任何其他方式保留特定对象的永久副本测试数据库? 这主要是为了提高测试速度。

我的设计控制器rspec中的“post create”问题

[好吧……我的第一个问题,所以要温柔。] 我正在使用设计进行身份validation,但我有自己的控制器来扩展创建用户时发生的事情。 我在注册时(注册)创建了“用户”和“代理商”。 在路线…… devise_for :users, :controllers => {:registrations => “registrations”} 我的完整控制器…… class RegistrationsController < Devise::RegistrationsController def create super # creates the @user @agency = Agency.create! params[:agency] @agency.users << @user @agency.owner = @user @user.agency = @agency @agency.save @user.account_admin = true @user.save end end 我的问题是我想设置一个rspec来检查这段代码。 代码似乎正在工作,但我在我的规格中拍摄100%的代码覆盖率。 这是我的整个规范…… require ‘spec_helper’ describe RegistrationsController do render_views describe “POST create” […]