标签: rspec-rails

RSpec 测试 Time 对象

我的时间相关规格不合格。

我知道时间是从纪元开始计算的,但如果对象表示足够接近,我希望这个规范能够通过。

例如:

expect( @model.end_date ).to eq end_date

expected: Wed, 08 Feb 2017 20:13:30 UTC +00:00
     got: Wed, 08 Feb 2017 20:13:30 UTC +00:00
Run Code Online (Sandbox Code Playgroud)

有人对测试时间有什么建议,这样轻微的偏移就不会导致测试失败吗?

ruby-on-rails rspec-rails

2
推荐指数
1
解决办法
2064
查看次数

如何在 rspec 测试之间保持 puma 服务器运行

我正在使用 rspec 和 capybara 来测试包含 javascript 的功能规范。每次我运行测试时,都会启动 puma 服务器的一个单独实例,例如

Capybara starting Puma...
* Version 3.11.0 , codename: Love Song
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:64558
Run Code Online (Sandbox Code Playgroud)

如果我运行完整的测试套件,则 puma 的此实例仅启动一次,但如果我正在调试特定测试,则每次都会运行它,这会大大增加调试所需的总时间。有什么方法可以启动测试 puma Web 服务器的实例并使其在各个测试运行之间保持运行吗?

capybara rspec-rails puma

2
推荐指数
1
解决办法
1074
查看次数

针对某些特定 RSpec 测试的自定义 VCR 请求匹配选项

我的 Rails 项目中有很多 RSpec 测试,用于测试对外部 REST API 的 HTTP 调用,并使用 VCR 磁带记录请求和响应。目前我的 VCR 配置如下:

VCR.configure do |c|
  c.cassette_library_dir = 'spec/vcr_cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
end
Run Code Online (Sandbox Code Playgroud)

因此请求匹配规则仅匹配 HTTP 方法和 URI。我想更改此设置以匹配请求正文:

VCR.configure do |c|
  c.cassette_library_dir = 'spec/vcr_cassettes'
  c.hook_into :webmock
  c.configure_rspec_metadata!
  c.default_cassette_options = {
    :match_requests_on => [:uri, :method, :body],
  }
end
Run Code Online (Sandbox Code Playgroud)

但由于我的项目中已经有很多测试,我想逐步进行,仅对某些测试激活此新限制,这样其他测试(使用旧磁带)就不会中断。

有没有办法将参数传递给 RSpec 测试,以便仅针对某些特定测试或测试组制定自定义请求匹配规则?

我想象类似的事情

it 'reverts transaction', :vcr, :body_matching => true do
    # something
end
Run Code Online (Sandbox Code Playgroud)

然后根据body_matching参数动态更改设置。

ruby-on-rails rspec-rails vcr

2
推荐指数
1
解决办法
2056
查看次数

pundit rspec - 未初始化的常量 UserPolicy,为什么?

寻求一些帮助,我是 Rspec 和 pundit 的菜鸟,我正在按照一个示例来设置 pundit 并对其进行测试,但所有测试都失败了

NameError:
  uninitialized constant UserPolicy
# ./spec/policies/user_policy_spec.rb:1:in `<top (required)>'
No examples found.
Run Code Online (Sandbox Code Playgroud)

需要一些帮助来找出原因,因为我不知道:(

使用:

  • 轨道 5.1.6
  • R规格3.7
    • rspec-核心 3.7.1
    • rspec-期望 3.7.0
    • rspec-模拟 3.7.0
    • rspec-rails 3.7.2
    • rspec 支持 3.7.1
  • 专家1.1.0
  • 设计4.4.3

配置/初始化器/pundit.rb

module PunditHelper
  extend ActiveSupport::Concern

  included do
    include Pundit
    rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
  end

  private

  def user_not_authorized
    flash[:alert] = "Access denied."
    redirect_to (request.referrer || root_path)
  end

end

ApplicationController.send :include, PunditHelper
Run Code Online (Sandbox Code Playgroud)

应用程序/策略/user_policy.rb

class UserPolicy
  include ApplicationHelper

  attr_reader :current_user, :model

  def initialize(current_user, model)
    @current_user = current_user …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails devise rspec-rails pundit

2
推荐指数
1
解决办法
1738
查看次数

Sidekiq::测试.假的!不伪造 Sidekiq::Queue

我有一个简单的工作人员正在访问其自己的队列的大小:

需要“sidekiq/api”

class TestWorker
  include Sidekiq::Worker

  def perform(*args)
    Sidekiq::Queue.new('test').size
  end
end
Run Code Online (Sandbox Code Playgroud)

然后我正在测试这个工人:

require 'rails_helper'

RSpec.describe TestWorker, type: :worker do
  describe '#perform' do
    it 'executes the job without connecting to Redis in the testing environment' do
      require 'sidekiq/testing'
      Sidekiq::Testing.fake! do
        TestWorker.perform_async
        TestWorker.drain
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

此测试失败,因为它尝试访问 redis:

1) TestWorker#perform executes the job without connecting to Redis in the testing environment
     Failure/Error: Sidekiq::Queue.new('test').size

     Redis::CannotConnectError:
       Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:344:in `rescue in establish_connection'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:328:in `establish_connection'
     # /home/matthieu/.rvm/gems/ruby-2.4.3/gems/redis-4.0.1/lib/redis/client.rb:99:in …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails rspec-rails sidekiq rspec-sidekiq

2
推荐指数
1
解决办法
5857
查看次数

如何使用 RSpec 测试 Sidekiq Worker?

我正在使用 RSPEC 进行测试,并使用 Sidekiq 进行后台作业。

因为 rspec 中没有生成器workers,所以不知道该使用什么。

https://relishapp.com/rspec/rspec-rails/docs/generators

require 'spec_helper'

RSpec.describe TestWorker, type: ? do # ex. :worker :sidekiq ...
  describe "TestWorker" do
    it "" do
     ....
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

bundle exec rspec spec/workers/test_worker_spec.rb

如下所示,我得到:uninitialized constant TestWorker require 'spec_helper'

describe TestWorker do
  it "" do
   ....
  end
end
Run Code Online (Sandbox Code Playgroud)

正如我所尝试的,gem rspec-sidekiq https://github.com/philostler/rspec-sidekiq

有人可以提供一个用于app/workers/在 Rspec 中进行测试的示例模板吗?

谢谢。

rspec ruby-on-rails rspec-rails sidekiq rspec-sidekiq

2
推荐指数
1
解决办法
8920
查看次数

RSpec 二等分无限期运行

这个问题与此不同:rspec Bisect Runs Indefinitely

\n\n

我的测试套件需要 \xc2\xb1 10 分钟才能运行。\n当我运行时bundle exec rspec --bisect=verbose,它卡住了 1 个多小时(这是我等待的最长时间):

\n\n
bundle exec rspec --bisect=verbose\nAssetSync: using /home/belasis/dev/deploy_webapp/config/initializers/asset_sync.rb\nBisect started using options: "" and bisect runner: :fork\nRunning suite to find failures...\n
Run Code Online (Sandbox Code Playgroud)\n\n

我点击后Ctrl C,出现此消息:

\n\n
Bisect aborted!\n\nThe most minimal reproduction command discovered so far is:\n  (Not yet enough information to provide any repro command)\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,问题在于它没有运行任何内容并且没有给出错误消息。这怎么可能?

\n

rspec rspec-rails rspec3

2
推荐指数
1
解决办法
1103
查看次数

ActionController :: UrlGenerationError:缺少必需的键:[:id]

我正在运行RSpec 3测试,并且在这个特定路径上遇到同样的错误:

 Failure/Error: visit book_path
 ActionController::UrlGenerationError:
 No route matches {:action=>"show", :controller=>"books"} missing required keys: [:id]
Run Code Online (Sandbox Code Playgroud)

我的测试还没完成,所以我确定后面的一些代码可能不正确.但我无法让我的访问路径行运行:

...
book = FactoryGirl.build(:book)
reviews = FactoryGirl.build_list(:review, 5, book: book)

visit book_path

reviews.each do |review|
  expect(page).to have_content(review.rating)
  expect(page).to have_content(review.body)
  expect(page).to have_content(review.timestamp)
end

  expect(page).to have_content('House of Leaves')
  expect(page).to have_content('Mark Z. Danielewski')
  expect(page).to have_content('Horror')
end
Run Code Online (Sandbox Code Playgroud)

在我的控制器中,我将show定义为:

def show
  @book = Book.find(params[:id])
  @reviews = @book.reviews.order('created_at DESC')
  @review = Review.new
end
Run Code Online (Sandbox Code Playgroud)

我的资源:

resources :books, only: [:index, :show, :new, :create] do
  resources :reviews, only: [:show, :new, :create] do
end
Run Code Online (Sandbox Code Playgroud)

rspec capybara rspec-rails ruby-on-rails-4 factory-bot

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

无法在Rails 5上安装RSpec - 找不到生成器'rspec:install'

我的Gemfile有以下内容:

source 'https://rubygems.org'
ruby '2.3.1'

gem 'rails', '>= 5.0.0.rc2', '< 5.1'

... more gems

group :development, :test do
  gem 'rspec-rails', '~> 3.5'
  gem 'factory_girl_rails', '~> 4.7'
  gem 'byebug'

end

group :test do
  gem 'faker', '~> 1.6', '>= 1.6.3'
  gem 'capybara', '~> 2.7', '>= 2.7.1'
  gem 'database_cleaner', '~> 1.5', '>= 1.5.3'
  gem 'launchy', '~> 2.4', '>= 2.4.3'
  gem 'selenium-webdriver', '~> 2.53', '>= 2.53.4'
  gem 'shoulda-matchers', '~> 3.1', '>= 3.1.1'


end
Run Code Online (Sandbox Code Playgroud)

当我运行rails生成rspec安装时,我得到了

Expected string default value for '--helper'; got true (boolean)
Expected …
Run Code Online (Sandbox Code Playgroud)

rspec rspec-rails

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

Rails请求规范发布语法

我似乎对如何在请求规范测试中发出发布请求以测试url不太了解,这是测试代码

RSpec.describe "Certifications", type: :request do
  describe "denies public access" do

    it "for new certification form" do
        get new_certification_path
        expect(response).to redirect_to new_user_session_path
    end

    it "for creating certification" do
        certification_attributes = FactoryGirl.attributes_for :certification 

        expect {
            post "/certifications", { certification: certification_attributes }
        }.to_not change(Certification, :count)

        expect(response).to redirect_to new_user_session_path
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

这给出了错误

  1) Certifications denies public access for creating certification
     Failure/Error: post "/certifications", { certification: certification_attributes }

 ArgumentError:
   unknown keyword: certification
Run Code Online (Sandbox Code Playgroud)

我已经尝试过:certifications => certification_attributes,基本上无法理解如何传递参数。

被测控制器仅向该帖子添加相关方法。

class CertificationsController < ApplicationController
  skip_before_action …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails http-post rspec-rails

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