我一直在使用 rails 5.0.0.1,我是自动化测试的新手,并且使用 rspec-rails-3.5.2 来编写自动化。
我想测试一些基本的控制器渲染功能和实例变量分配。编写了一个控制器测试用例,它将检查是否呈现了 get_structure 模板并分配了所需的实例变量。
describe 'GET #get_structure' do
context 'get attributes to build tree structure' do
let(:plan) { FactoryGirl.create(:plan) }
it 'expects all keys to build structure' do
get :get_structure, params: { id: 6, school_subdomain: 'chrys' }
expect(response).to render_template(:get_structure)
expect(assigns.keys.include?('collection')).to be true
expect(assigns.keys.include?('structure')).to be true
expect(assigns.keys.include?('config')).to be true
end
end
end
Run Code Online (Sandbox Code Playgroud)
一旦我运行了测试用例,我意识到由于某些安全原因,assert_template 不再受支持。
NoMethodError: assert_template 已被提取到 gem 中。要继续使用它,请添加
gem 'rails-controller-testing'到您的 Gemfile。
由于它在 rspec-rails 文档中提到使用 rails-controller-testing gem 将添加回功能,我确实添加了它。
在 Rails 5.x 中,控制器测试已移至其自己的 gem 中,即 rails-controller-testing。在您的控制器规范中使用分配而不添加此 gem …
像许多非 Greenfield 项目一样,我到达新工作时发现大多数测试都是使用功能规范而不是控制器/模型规范来执行的。当然,这意味着测试需要很长时间才能执行。
有没有一种方法可以设置 RSpec,以便我的功能测试最后运行 - 并且只有在所有其他测试首先成功时它们才会运行。我正在将应用程序迁移到控制器测试以加快执行速度,但如果模型验证出现问题或我的控制器之一损坏,则无需等待 15 分钟来完成功能测试。
我想做的事情可能吗?
(我现在正在 RubyMine 中运行测试;但rake spec如果需要的话我可以转移到)。
在 Rspec 中,我想利用 usingsuper()来调用已定义的let块(如果存在)或设置新值(如果不存在),我想在组中使用它,shared_example但我只是找不到如何执行此操作。
我尝试过检查是否@some_let存在,我尝试过检查是否:some_let拥有 的超级方法kernel,但它们都没有提供任何有用的东西;我无法访问instance_methods,或者instance_method因为 Rspec 不允许我访问,并且在互联网上搜索方法尚未找到答案。
我希望能够做这样的事情:
shared_examples 'a shared example' do
let(:some_let) { let_exists?(:some_let) ? super() : some_new_value }
end
Run Code Online (Sandbox Code Playgroud)
有类似的方法let_exists?或类似的效果吗?
从 Rails 6.0.3 升级到 6.1 时,我遇到了这个错误:
NoMethodError:
undefined method `assert_nothing_raised' for #<RSpec::ExampleGroups::EmailJob:0x00005572d8a00758>
Did you mean? assert_raises
Run Code Online (Sandbox Code Playgroud)
每次测试调用时都会发生这种情况perform_enqueued_jobs。我正在使用 RSpec 3.9。
我正在尝试使用RSpec测试Hour模型,即类似方法'find_days_with_no_hours',其行为类似于范围.业务有多个与STI相关的营业时间.需要通过Business对象调用find_days_with_no_hours,我无法弄清楚如何在RSpec测试中设置它.我希望能够测试类似于:
bh = @business.hours.find_days_with_no_hours
bh.length.should == 2
Run Code Online (Sandbox Code Playgroud)
我尝试了各种方法,比如创建一个Business对象(比如Business.create),然后设置@ business.hours << mock_model(BusinessHour,...,...,...)但是没有工作.
这通常是怎么做的?
class Business < ActiveRecord::Base
has_many :hours, :as => :hourable
end
class Hour < ActiveRecord::Base
belongs_to :hourable, :polymorphic => true
def self.find_days_with_no_hours
where("start_time IS NULL")
end
end
Run Code Online (Sandbox Code Playgroud) 我被困在RSpec的一个规格上.如何更新模型的属性以使其为零?最好在更新控制器规范下验证?
下面是一个示例代码.
describe User do
describe ".validation" do
before(:each) do
@user = User.create!({
:username => "dexter_morgan"
})
end
...
context "given invalid attributes" do
# how can I make the username nil?
it "rejects blank username"
end
end
end
Run Code Online (Sandbox Code Playgroud) 我的rspec测试似乎运行速度非常慢,即使有防护和spork.
Finished in 5.36 seconds
13 examples, 2 failures
Run Code Online (Sandbox Code Playgroud)
我知道我可以做几件事来优化我的测试并减少与数据库的交互,但我强烈怀疑spec_helper设置不正确.我在轨道3.2.11与mongoid.每次运行后数据库清理程序都会清理.
spec_helper.rb
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
DatabaseCleaner[:mongoid].strategy = :truncation
RSpec.configure do |config|
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.filter_run focus: true
config.filter_run_excluding :remove => true
config.run_all_when_everything_filtered = true
config.include Mongoid::Matchers
config.include Capybara::DSL
ActiveSupport::Dependencies.clear
end
end
Spork.each_run do
Fabrication.clear_definitions
RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.clean
end
end
end
Run Code Online (Sandbox Code Playgroud)
更新:问题出在我的一个测试中.这需要3秒钟.请查看@Sam Peacey的答案,了解我用于获得以下结果的命令
Dynamic Model should …Run Code Online (Sandbox Code Playgroud) ruby-on-rails spork rspec-rails ruby-on-rails-3 database-cleaner
我正在尝试使用RSpec的请求规范将主机更改为指向远程URL而不是localhost:3000.如果可能,请告诉我.
注意:只是想提一下远程URL只是一个JSON API.
我正在用RSpec测试我的观点.由于一些变化,现在url_for在视图中调用了我为show动作编写的所有规范都失败了:
No route matches {:controller=>"events", :action=>"show"}
Run Code Online (Sandbox Code Playgroud)
该:id部分缺失以及由于呼叫失败.
(我知道我可以将那个失败的方法调用存根)
我发现的唯一的东西很旧,看起来像一个糟糕的解决方法.另外,RSpec的文件没有显示什么帮助.
有没有正确的方法告诉RSpec它应该是什么样的events/123?
更多背景:
在视图中我调用了一个帮助器,如下所示:
def some_helper_method
url_for(only_path: false)} + "something_else"
end
Run Code Online (Sandbox Code Playgroud)
对该方法的调用失败了
Failure/Error: render
ActionView::Template::Error:
No route matches {:controller=>"events", :action=>"show"}
Run Code Online (Sandbox Code Playgroud)
我正在通过对该方法的调用进行存根来修复它 view.stub(some_helper_method: 'SOME_URL')
我是RSpec和Cucumber BDD开发的新手,这是一个很大的飞跃.
使用rake生成脚手架model_name我已经制作了大量的代码似乎有些意义,但我并不十分自信.
在我添加一些数据库关系和模型验证之前,我一直在使用RSpec,然后这一切都爆炸了,我真的不知道修复它的最佳实践或最干净的方法是什么.
很明显我的问题是"有效属性"需要用链接到有效的患者/麻醉师/程序/外科医生的外键来定义,但我不知道我应该如何在RSpec中写这个(可能使用FactoryGirl来生成有效的每个模型的对象?)模型RSpec代码对我有意义,但控制器代码超出了我的深度.
看起来它应该可以在一行,或者可能是四行(每个对象一行),但应该是它.
代码
#app/models/procedure.rb
class Procedure < ActiveRecord::Base
belongs_to :patient
belongs_to :surgeon
belongs_to :anaesthetist
belongs_to :hospital
validates :patient_id, presence: true
validates :surgeon_id, presence: true
validates :anaesthetist_id, presence: true
validates :hospital_id, presence: true
end
Run Code Online (Sandbox Code Playgroud)
#spec/models/procedure_spec.rb
require 'spec_helper'
describe Procedure do
it { should validate_presence_of(:patient_id) }
it { should validate_presence_of(:surgeon_id) }
it { should validate_presence_of(:anaesthetist_id) }
it { should validate_presence_of(:hospital_id) }
end
Run Code Online (Sandbox Code Playgroud)
#spec/controllers/procedures_controller_spec.rb
describe ProceduresController do
# This should return the minimal set of attributes required to create …Run Code Online (Sandbox Code Playgroud)