标签: rspec3

在RSpec中获得"范围的不良价值"

这是我的代码:

#app/models/user.rb
class User < ActiveRecord::Base

  def display_name
    name.split(' ').try(:first)
  end
end

#spec/models/user_spec.rb
require 'rails_helper'

RSpec.describe User, :type => :model do
  let(:user) { FactoryGirl.create :user }

  it "should return the correct display name" do
    expect ( user.display_name ).to eql("Chuck")
  end
end

#spec/factories/users.rb
FactoryGirl.define do
  factory :user do
    provider "MyString"
    uid "MyString"
    name "Chuck Norris"
    oauth_token "MyString"
    oauth_expires_at "2014-07-24 07:31:36"
  end
end 
Run Code Online (Sandbox Code Playgroud)

但是,当我运行此规范时,我收到以下错误:

Failure/Error: expect ( user.display_name ).to eql("Chuck")
     ArgumentError:
       bad value for range
Run Code Online (Sandbox Code Playgroud)

它似乎to从rails 检查方法active support,但我认为 …

ruby rspec ruby-on-rails rspec3

8
推荐指数
1
解决办法
5824
查看次数

Capybara :: ElementNotFound:无法找到文件字段"file"

我正在测试文件上传即CSV.在我的代码以及浏览器HTML中,我找到了文件字段,但在测试capybara时无法找到文件字段.我努力尝试不同的方法,但无法解决问题.这部分看起来像这样:

#add_file_box
  %div.msg
  %h1.page-header
    = "Upload a CSV"
  %h4.title

  = form_tag dummy_path, multipart: true, class: "upload_csv" do
    = hidden_field_tag :dmp_id, @dmp.id
    .form-group
      .input-group
        %span.input-group-btn
          %span.btn.btn-primary.btn-file
            Choose file
            = file_field_tag :file, style: 'line-height: normal',  accept: "text/csv", class: "file_input"
        %input.form-control.input-custom{:readonly => "", :type => "text"}
    .form-group
      = submit_tag "Upload CSV", class: "btn btn-primary", id: "upload_csv"
Run Code Online (Sandbox Code Playgroud)

而且水豚测试看起来像这样

 within '.upload_csv' do
     page.attach_file('file', "#{Rails.root}/spec/acceptance/test_uploads/input_output.csv")
     click_button 'Upload'
   end
Run Code Online (Sandbox Code Playgroud)

如果你能帮助我解决这个问题,我将感激不尽?

ruby ruby-on-rails capybara rspec3

8
推荐指数
1
解决办法
4831
查看次数

如何发现为什么 RSpec 需要这么长时间才能启动?

我知道有一种方法可以知道哪些规格占用的时间最多,但我对 RSpec 的加载感到怀疑。规格本身并不会花费太多时间,但负载却花费了太多时间。
有办法发现吗?
我正在研究 Rails 的遗留代码,但我不知道哪些 gem 可能会影响它。

rspec ruby-on-rails rspec-rails rspec3

8
推荐指数
1
解决办法
3353
查看次数

Rails/RSpec切换缓存以进行单个测试

因此,在我的应用程序中,我可以禁用所有测试的缓存,这将是理想的,但显然有许多遗留测试依赖于缓存功能.有没有办法为单个RSpec测试启用Rails缓存?

就像是:

before(:each) do
  @cache_setting = Rails.cache.null_cache
  Rails.cache.null_cache = true
end

after(:each) do
  Rails.cache.null_cache = @cache_setting
end

it 'does not hit the cache' do
  ...
end
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails ruby-on-rails-4 rspec3

7
推荐指数
1
解决办法
1033
查看次数

Rspec:期望“super”被调用

我有一个很短的函数要测试:

def my_fn
  if some_condition
    super(@my_attr)
  end
end
Run Code Online (Sandbox Code Playgroud)

我希望我的规范能够验证是否super被调用,但我无法执行以下操作,因为:super似乎不是发送的消息:

    expect(subject).to receive(:super)
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails rspec3

7
推荐指数
0
解决办法
1960
查看次数

RSpec中单个设置的多个断言

我有一些较慢的规格,我想优化.这种规范的例子如下:

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
Run Code Online (Sandbox Code Playgroud)

这是非常易读的,我对它很满意,除了每个额外的规范都会在总运行时间内增加至少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' …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails rspec2 rspec3

6
推荐指数
1
解决办法
775
查看次数

RSpec 3:期望 object.do_something 不会引发特定类型的错误

我想测试一个特定的方法是否不会引发类 AError 的错误。它可以引发 BError、ArgumentError、几乎任何其他类型的错误,或者没有错误,只是不是 AError。是否有任何未弃用的(从 RSpec 3 开始)方法来做到这一点?

我试过

expect { object.do_something }.not_to raise_error(AError)
Run Code Online (Sandbox Code Playgroud)

但我明白了

ArgumentError:
   `expect { }.not_to raise_error(SpecificErrorClass)` is not valid,
    use `expect { }.not_to raise_error` (with no args) instead
Run Code Online (Sandbox Code Playgroud)

无参数方法的问题在于,当它应该传递除 AError 之外的任何内容时,测试将因任何类型的错误而失败。

该文档似乎没有帮助:https : //www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/raise-error-matcher

老版本的RSpec好像有办法处理这种情况,不明白新版本的RSpec怎么了。

所以我很困惑。谢谢。

ruby rspec3

6
推荐指数
1
解决办法
5603
查看次数

Rails 4,RSpec 3.2 - 如何模拟ActionMailer的deliver_now方法来引发异常

环境

Ruby 2.2.1,Rails 4.2.0,rspec-core 3.2.2,rspec-expectations 3.2.0,rspec-mocks 3.2.1,rspec-rails 3.2.1,rspec-support 3.2.2

我有以下方法

def send_event_alert_email(event_id)
  event = Event.find_by(id: event_id)
  ...
  ...     
  ...
  EventAlertMailer.event_alert(event_id).deliver_now

  create_alert(event)
end
Run Code Online (Sandbox Code Playgroud)

我需要编写规范,以确保create_alert(event)EventAlertMailer.event_alert(event_id).deliver_now引发任何异常时不会调用它.所以基本的问题是我如何模拟deliver_now以引发它可能实际抛出的可能异常.

actionmailer rspec3 ruby-on-rails-4.2 ruby-2.2

6
推荐指数
1
解决办法
1135
查看次数

如何解决RSpec关于新期望语法的弃用警告?

当我运行这个RSpec示例时,它会通过,但我收到了弃用警告.

context "should have valid detail for signup" do
  it "should give error for invalid confirm password" do
    find("#usernamesignup").set("Any_name")
    find("#mobile_number").set("1234567890")
    find("#emailsignup").set("mymail@yopmail.com")
    find("#passwordsignup").set("12345678")
    find("#passwordsignup_confirm").set("")
    find(".signin").click
    sleep 2
    page.should have_content "Confirm Password Does not Match"
  end
end
Run Code Online (Sandbox Code Playgroud)

这是输出:

弃用警告:

不推荐使用shouldrspec-expectations的旧:should语法而不显式启用语法.使用新的:expect 语法或明确启用:shouldconfig.expect_with(:rspec) { |c| c.syntax = :should }替代.来自/home/rails/rails_nimish/Devise_use/spec/features/users_spec.rb:113:in`block(4 levels)in'

在此输入图像描述

如何解决此警告?

更新: 我刚刚更换的解决方案

page.should have_content"确认密码不匹配"

有:

expect(page).to have_content "Confirm Password Does not Match"
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails capybara rspec-rails rspec3

6
推荐指数
2
解决办法
3278
查看次数

RSpec 3 RuntimeError:“让声明在 `before(:context)` 挂钩中访问”

这是我的错误

Failure/Error: @queue = FactoryGirl.create(model.to_s.underscore.to_sym)
 RuntimeError:
   let declaration `model` accessed in a `before(:context)` hook at:
     /var/www/html/SQ-UI/spec/support/user_queue/asterisk_serialize_spec.rb:7:in `block (2 levels) in <top (required)>'

   `let` and `subject` declarations are not intended to be called
   in a `before(:context)` hook, as they exist to define state that
   is reset between each example, while `before(:context)` exists to
   define state that is shared across examples in an example group.enter code here
Run Code Online (Sandbox Code Playgroud)

这是破坏的代码

let(:model) { described_class } # the class that includes the concern

before(:all) do
  @queue …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails rspec-rails rspec3

6
推荐指数
1
解决办法
4565
查看次数