我有以下模型,我想用RSpec测试:
class Language < ActiveRecord::Base
  has_and_belongs_to_many :dvds
  validates :title, presence: true, uniqueness: { case_sensitive: false }
end
language_spec.rb
describe Language do
  describe 'title validation' do
    context 'title is present' do
      before(:each) do
        @lang = Language.new(title: 'English')
      end
      it 'is valid with present title' do
        expect(@lang).to have_exactly(0).errors_on(:title)
      end
    end
    context 'title is not present' do
      before(:each) do
        @lang = Language.create
      end
      it 'has an error on title attribute' do
        expect(@lang).to have_exactly(1).errors_on(:title)
      end
    end
  end
end
不幸的是我遇到了测试失败:
失败:
1)语言标题验证标题存在对当前标题有效失败/错误:期望(@lang).to have_exactly(0).errors_on(:title)NoMethodError:未定义的方法
errors_on' for …
我正在创建一个对象,subject并且需要测试是否会引发异常.以下代码说明了我正在尝试实现的目标:
describe MyClass do
  describe '#initialize' do
    subject { MyClass.new }
    it { is_expected.not_to raise_error(Some::Error) }
  end
end
我有一种感觉,我会以错误的方式来做这件事.在subject不创建对象两次的情况下,将对象设置为新对象的首选方法是什么?
更新
我的问题是双重的.首先,这种语法不起作用:
it { is_expected.not_to raise_error }
然而,expect在一个it区块内部使用(正如Jimmy Cuadra所指出的那样):
it 'does not raise an error' do
  expect { subject }.not_to raise_error
end
我不熟悉RSpec告诉你为什么会这样.
其次,自RSpec 3.0.0.beta1以来,可以使用raise_error特定的错误类.因此,以下内容无效:
expect { subject }.to raise_error(Some::Error)
有关更多信息,请参阅
我有一些较慢的规格,我想优化.这种规范的例子如下:
require 'rspec'
class HeavyComputation
  def compute_result
    sleep 1 # something compute heavy here
    "very big string"
  end
end
describe HeavyComputation, 'preferred style, but slow' do
  subject { described_class.new.compute_result }
  it { should include 'big' }
  it { should match 'string' }
  it { should match /very/ }
  # +50 others
end
这是非常易读的,我对它很满意,除了每个额外的规范都会在总运行时间内增加至少1秒.这是不可接受的.
(请不要讨论HeavyComputation课程的优化,因为它超出了本问题的范围).
所以我要求的是这样的规格:
describe HeavyComputation, 'faster, but ugly' do
  subject { described_class.new.compute_result }
  it 'should have expected result overall' do
    should include 'big'
    should match 'string' …学习如何使用Rspec 3.我对匹配器有疑问.我关注的教程基于Rspec 2.
describe Team do
  it "has a name" do
    #Team.new("Random name").should respond_to :name
    expect { Team.new("Random name") }.to be(:name)
  end
  it "has a list of players" do
    #Team.new("Random name").players.should be_kind_of Array
    expect { Team.new("Random name").players }.to be_kind_of(Array)
  end
end
为什么代码导致错误,而我注释掉了通过折旧警告.
错误
Failures:
  1) Team has a name
     Failure/Error: expect { Team.new("Random name") }.to be(:name)
       You must pass an argument rather than a block to use the provided matcher (equal :name), or the matcher must implement `supports_block_expectations?`.
     # ./spec/team_spec.rb:7:in …我正在将我的Capybara测试套件从capybara-webkit转换为poltergeist.
-require 'capybara/webkit'
+require 'capybara/poltergeist'
-Capybara.javascript_driver = :webkit
+Capybara.javascript_driver = :poltergeist
在运行我的测试时,第一次测试失败并显示此消息...
Failure/Error: Unable to find matching line from backtrace
 ActionController::RoutingError:
   No route matches [GET] "/assets/fonts/glyphicons-halflings-regular.ttf"
经过一番调查,我发现类似的问题在这里.
但是,我有一个bootstrap.css的手动安装.我按照第二个答案中的建议调整了路径,并收到了更新路径的相同消息.
经过一段时间和挫折之后,我意识到这只是我套房中的第一次测试失败,其余的都通过了.很自然地,我删除了第一个测试.现在,"新"第一次测试失败,其余测试通过.所以我把它们全部删除了.现在,他们都没有失败!测试很简单!结束.
是什么让我的第一次测试失败?为什么其余的都过去了?
更多信息请求.
我正在使用turbolinks-ios和Rails变体开发iOS应用程序.
在我的iOS应用程序,我设置自定义用户代理iPadApp和检测on Rails的application controller使用request.user_agent.try(:index, 'iPadApp')设置variant到tablet(因为观点一样foo.html+tablet.haml).
我的应用程序工作正常,现在我正在尝试为我的应用程序编写功能测试,但无法正确设置用户代理.我试过这篇帖子实际上是stackoverflow但是我发现它没有设置request.user_agent而是用(page.driver.browser.header(key, value))设置查询参数request.params.
在我的控制器测试中,我只是request.user_agent = 'iPadApp'用来设置工作正常的用户代理.
如何配置测试请求以便我可以使用request.user_agent.try(:index, 'iPadApp')?
谢谢你的帮助.
我正在尝试使用 rspec 3.0 测试 own_to 关联,但我不断遇到错误
NoMethodError:RSpec::Core 的未定义方法“belong_to”
  it "should belong to a user" do
ride = Ride.new
user = User.new
user.rides << ride
expect(ride).to belong_to user
结尾
我在 rspec 3.0 的文档中找不到任何用于测试高级关联的内容。请帮助!
我的模型中的某些属性具有存在验证,我想在我的规范中添加测试以检查当属性为空时是否生成错误。
我正在使用此代码:
it 'should have a name' do
    expect(@patient.errors[:name].size).to eq(1)
end
但这是 rspec 命令的结果:
失败:
  1) 患者应有姓名
     失败/错误:expect(@patient.errors[:name].size).to eq(1)
       预期:1
            得到:0
       (比较使用 ==)
     # ./spec/models/patient_spec.rb:11:in `block (2 levels) in '
在 0.03002 秒内完成(文件需要 40.54 秒加载)
1个例子,1个失败
失败的例子:
rspec ./spec/models/patient_spec.rb:10 # 患者应该有一个名字
 RSpec 的文档提到了--bisect 选项,该选项在运行时提供最小的复制,例如
rspec ./spec/calculator_10_spec.rb[1:1] ./spec/calculator_1_spec.rb[1:1] --seed 1234
比特是什么[1:1]意思?
我正在开发一个具有 500 多个规格的应用程序,当我在本地计算机上运行这些规格时,它成功通过了。但当这些规范在 CircleCI 上运行时,该进程就会被终止。我尝试通过跟踪本地计算机上的内存来调查该问题。当我看到 ruby 进程占用了 4GB 内存时,我感到很惊讶,这就是触发 CircleCI 杀死该进程的原因。
我不确定我的规格占用所有这些内存的原因我已经搜索过在每个规格后清理内存的配置但无济于事。
这是我的rails_helper.rb
require "mongoid-rspec"
require "spec_helper"
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
if Rails.env.production?
  abort("The Rails environment is running in production mode!")
end
require "database_cleaner"
require "rspec/rails"
#
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
  # FactoryGirl
  config.include FactoryGirl::Syntax::Methods
  # Render
  config.render_views
  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
  config.include Mongoid::Matchers, type: …rspec3 ×10
rspec ×6
ruby ×4
rspec-rails ×3
capybara ×1
circleci ×1
poltergeist ×1
rspec2 ×1
turbolinks ×1