Tag: rspec

RSpec和ActiveModel

我有一个模块,它包含activemodel,我想用rspec测试它。 这是我到目前为止的设置: lib/ |__ my_module/ | |__ base.rb |__ my_module.rb spec/ |__ my_module_spec.rb |__ spec_helper.rb |__ support/ |__ shared_examples/ |__ active_model.rb 在’my_class.rb’里面: require “active_model” require “my_module/base” 在’base.rb’里面: module MyModule class Base extend ActiveModel::Naming include ActiveModel::Conversion include ActiveModel::Validations end end 在’my_module_spec.rb’里面: require ‘spec_helper’ describe MyModule do describe MyModule::Base do it_behaves_like “ActiveModel” end end 在’spec_helper.rb’里面: $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), ‘..’, […]

何时在BDD循环中从黄瓜切换到rspec以进行登录过程

我还在尝试理解BDD循环中cucumber和rspec的组合。 我为一个非常简单的登录系统定义了以下场景: Feature: Log in In order to get access to the application As a user I want to log in Scenario: User logs in successfully Given I exist as a user When I go to the login page And I fill in “username” with “gabrielhilal” And I fill in “password” with “secret” And I […]

RSpec Rails登录filter

我最近开始使用我的Rails(3.0.8)应用程序使用rspec-rails(2.6.1)。 我已经习惯了Test :: Unit,而且我似乎无法为我的测试方法提供filter。 我喜欢尽可能保持DRY,所以我想设置一个filter,我可以在调用测试方法之前调用任何将作为Authlogic用户登录的测试方法。 我尝试使用spec_helper.rb中的RSpecfilter来完成此操作: config.before(:each, :login_as_admin => true) do post “/user_sessions/create”, :user_session => {:username => “admin”, :password => “admin”} end 然后我在相应的测试方法中使用它(在本例中为spec / controllers / admin_controller_spec.rb): require ‘spec_helper’ describe AdminController do describe “GET index” do it(“gives a 200 response when visited as an admin”, :login_as_admin => true) do get :index response.code.should eq(“200”) end end […]

设置http状态代码

redirect_to browse_path(asset.parent_id), notice: “successfully created file!”, status: 201 201是您创建资源时应设置的状态。 虽然上述方法适用于创建操作,但其操作规范不再执行: subject { response } describe ‘.create’ do context ‘when orphan’ do before do post :create, asset: { parent_id: nil, uploaded_file: file } end it { should have_http_status 201 } it { should redirect_to ‘/’ } end end 状态期望通过,但redirect_to期望失败: Expected response to be a , but […]

为什么我的导轨规格不会运行?

当我尝试运行我的规范时,我得到一个未初始化的常量错误。 我的规格看起来像这样: describe Facility do it { should have_many(:units) } it { should have_many(:facilities_users) } it { should have_many(:administrators) } it { should have_many(:facility_employees) } end 错误是: facility_spec.rb:1:in `’: uninitialized constant Facility (NameError) 我当然有一个Facility模型,所以我不确定为什么会这样。

ActiveSupport :: Deprecation.silenced = true对我不起作用?

我的应用程序是使用Ruby 1.8.7和Rails 2.3.11开发的。 运行’rake spec’时,我收到很多弃用警告 DEPRECATION WARNING: ActiveSupport::Dependencies.load_paths is deprecated, please use autoload_paths instead. (called from load_paths at /home/soundarapandian/.rvm/gems/ruby-1.8.7-p352/gems/desert-0.5.2/lib/desert/manager.rb:36) DEPRECATION WARNING: ActiveSupport::Dependencies.load_paths is deprecated, please use autoload_paths instead. (called from load_paths at /home/soundarapandian/.rvm/gems/ruby-1.8.7-p352/gems/desert-0.5.2/lib/desert/manager.rb:36) DEPRECATION WARNING: ActiveSupport::Dependencies.load_paths is deprecated, please use autoload_paths instead. (called from load_paths at /home/soundarapandian/.rvm/gems/ruby-1.8.7-p352/gems/desert-0.5.2/lib/desert/manager.rb:36) DEPRECATION WARNING: ActiveSupport::Dependencies.load_paths is deprecated, please use autoload_paths […]

Guard Rspec:不推荐使用cli选项,更改为:cmd选项

在我的rails应用程序中使用Guardfile guard ‘rspec’, all_after_pass: false, cli: ‘–drb’ do 不工作Guardfile guard ‘rspec’, all_after_pass: false, cmd: ‘–drb’ do 我已经多次打开了bundle exec guard ,但我发现即使我收到此消息Guard::RSpec DEPRECATION WARNING: The :cli option is deprecated. Please customize the new :cmd option to fit your need. Guard::RSpec DEPRECATION WARNING: The :cli option is deprecated. Please customize the new :cmd option to fit your need. […]

在不加载ActiveRecord的情况下测试Rails模块

我正在研究一个Rails应用程序,我正在使用RSpec进行测试。 经过大量等待测试完成之后,我跟随了Gary Bernhardt的建议以及Paul Annesley的这篇信息性post并设置了一个分层spec_helper,我只加载了我绝对需要的Rails部分,以缩短测试时间,并提取function进入我单独测试的单独模块。 这是有道理的。 问题是我有一个模块(扩展ActiveSupport::Concern ),其中包含基于ActiveRecordfunction的实例和类方法,例如绑定到动态查找器,如find_and_create_by_ 。 到目前为止,我已经能够创建一个包含模块和测试的虚拟类,但现在我想将更多逻辑从我的ActiveRecord模型转移到模块中。 具体的例子可能是回调,validation器和方法委托之类的东西,所有这些都与它我正在访问的API有关。 虽然在我的测试中有两个选择,但我现在卡住了: 存根和/或模拟我在模块中调用的每个ActiveRecord方法,这将使测试保持快速但可能使测试代码非常复杂,或者 在测试中需要activerecord,让我的虚拟类inheritance自ActiveRecord::Base并且只测试模块,因为我会测试任何其他的rails模型,这会慢一些但保持测试代码干净。 后一种选择并不真正吸引我,因为我在模块中隔离代码的全部原因是我想将它与Rails分开。 我不是在寻找黑色或白色的答案,但有人会对这种情况有什么建议或最佳实践指示吗? 首先十分感谢!

需要Qt版本5的capybara-webkit的弃用警告

当我运行rspec时,我收到以下消息: The next major version of capybara-webkit will require at least version 5.0 of Qt. You’re using version 4.8.6. 我不知道我在使用Qt,所以不知道升级它的含义。 如何进行升级以及在此之前应采取哪些预防措施?

使用poltergeist和capybara创建一个post请求

我正在从由默认Rack驱动程序支持的Capybara过渡到由Poltergeist支持的Capybara。 出于令人沮丧的原因,我需要在加载页面之前操作会话数据。 但我无法弄清楚如何直接使用Rack驱动程序操作会话,所以我 Capybara.current_session.driver.submit :post, “/current_search”, {:session => :data } 不幸的是,Poltergeist driver对象没有submit方法,就像Rack驱动程序一样。 有三种可能性: 如何使用poltergeist / phantomjs提交POST请求? (实现这似乎是最简单的) 如何使用poltergeist / phantomjs直接操作会话数据? 我可以重构代码,以便不再需要直接操作会话数据。 这将是耗时的并且是低优先级的。 PS:由于使用Ruby 1.8,我们仍然坚持使用Poltergeist v1.0.2