标签: rspec-rails

如何获取rspec的ENV变量集?

我正在使用工头来启动我的rails开发服务器.我可以将所有环境变量放在.env文件中,这很好.有没有办法为我的测试环境做类似的事情?

我想设置一个我将与vcrgem 一起使用的API密钥,但我不想将API添加到版本控制中.除了在启动测试脚本时手动设置环境变量之外的任何建议?

testing rspec ruby-on-rails environment-variables rspec-rails

36
推荐指数
2
解决办法
3万
查看次数

显示每个rspec示例的运行时

目前我正在运行超过1k的例子,这需要很长时间才能完成(超过20分钟!!!).

我想确定哪些示例需要花费更多时间才能完成,是否有任何方法可以运行rspec并返回每个示例完成(单独)所需的时间?我正在使用rspec 1.3.0rspec-rails 1.2.3

tdd rspec ruby-on-rails rspec-rails

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

rails-rspec错误无法加载此类文件 - rspec/core/formatters/progress_formatter

我打破了我的rails-rspec.我切换到另一个gemset来运行第三方测试.当我返回到我的2.3.0(默认)gemset时,我有以下错误.

运行rspec得到:

/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require': cannot load such file -- rspec/core/formatters/progress_formatter (LoadError)
Run Code Online (Sandbox Code Playgroud)

运行rails生成rspec:install返回:

Could not find generator 'rspec:install'. Maybe you meant 'devise:install' or ...
Run Code Online (Sandbox Code Playgroud)

我尝试卸载并重新安装,但错误仍然存​​在.

运行rspec -v返回:

- rspec-core 3.6.0
- rspec-expectations 3.6.0
- rspec-mocks 3.6.0
- rspec-rails 3.6.1
- rspec-support 3.6.0
Run Code Online (Sandbox Code Playgroud)

看来红宝石找不到rspec-core.我试过这篇文章的解决方法但没有成功.提前感谢您提供的任何见解.

运行

rails 4.2.0,ruby 2.3.0

rspec-rails ruby-on-rails-4

30
推荐指数
3
解决办法
1万
查看次数

如何在rspec测试中包含lib目录

lib在JRuby 1.7.4上测试我的gem包含一个目录时遇到了问题.

我想测试位于的文件 lib/vger/resources/account_manager.rb

我的spec文件在 spec/vger/resources/account_manager_spec.rb

require 'spec_helper'

describe Vger::Resources::AccountManager do     
    .
    .
    end 
end
Run Code Online (Sandbox Code Playgroud)

我试图包含我想要测试的文件 spec_helper.rb

require 'rubygems'
require 'bundler/setup'
require 'vger/resources/account_manager'
require 'vger'

RSpec.configure do |config|
  # some (optional) config here
end
Run Code Online (Sandbox Code Playgroud)

在通过命令运行测试时,rspec spec/vger/resources/account_manager_spec.rb我收到此错误:

NameError: uninitialized constant Vger::Resources
    const_missing at org/jruby/RubyModule.java:2631
Run Code Online (Sandbox Code Playgroud)

我似乎想要测试的文件没有加载.请告诉我哪里出错了,我应该在哪里纠正.

ruby testing rspec rspec-rails

28
推荐指数
4
解决办法
3万
查看次数

如何从脚手架完成rspec put控制器测试

我正在使用脚手架来生成rspec控制器测试.默认情况下,它会将测试创建为:

  let(:valid_attributes) {
    skip("Add a hash of attributes valid for your model")
  }

  describe "PUT update" do
    describe "with valid params" do
      let(:new_attributes) {
        skip("Add a hash of attributes valid for your model")
      }

      it "updates the requested doctor" do
        company = Company.create! valid_attributes
        put :update, {:id => company.to_param, :company => new_attributes}, valid_session
        company.reload
        skip("Add assertions for updated state")
      end
Run Code Online (Sandbox Code Playgroud)

使用FactoryGirl,我已经填写了:

  let(:valid_attributes) { FactoryGirl.build(:company).attributes.symbolize_keys }

  describe "PUT update" do
    describe "with valid params" do
      let(:new_attributes) { FactoryGirl.build(:company, name: 'New Name').attributes.symbolize_keys …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails scaffolding rspec-rails factory-bot

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

模拟Rails.env.development?使用rspec

我正在使用rspec编写单元测试.

我想模仿Rails.env.develepment?返回真实.我怎么能实现这个目标?

我试过这个

Rails.env.stub(:development?, nil).and_return(true)
Run Code Online (Sandbox Code Playgroud)

它抛出了这个错误

activesupport-4.0.0/lib/active_support/string_inquirer.rb:22:in `method_missing': undefined method `any_instance' for "test":ActiveSupport::StringInquirer (NoMethodError)
Run Code Online (Sandbox Code Playgroud)

更新ruby版本ruby-2.0.0-p353,rails 4.0.0,rspec 2.11

describe "welcome_signup" do
    let(:mail) { Notifier.welcome_signup user }

    describe "in dev mode" do
      Rails.env.stub(:development?, nil).and_return(true)
      let(:mail) { Notifier.welcome_signup user }
      it "send an email to" do
        expect(mail.to).to eq([GlobalConstants::DEV_EMAIL_ADDRESS])
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

rspec rspec2 rspec-rails

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

如何在RSpec中测试attr_accessible字段

所以,我们一直在建立attr_accessibleattr_protected通过了我们的Rails应用程序3.2多领域.目前我们确实没有测试以确保这些字段受到保护.

所以我决定谷歌一些答案,并偶然发现这个解决方案:

RSpec::Matchers.define :be_accessible do |attribute|
  match do |response|
    response.send("#{attribute}=", :foo)
    response.send("#{attribute}").eql? :foo
  end
  description { "be accessible :#{attribute}" }
  failure_message_for_should { ":#{attribute} should be accessible" }
  failure_message_for_should_not { ":#{attribute} should not be accessible" }
end
Run Code Online (Sandbox Code Playgroud)

但是这个解决方案只测试方法是否响应.我需要的是一种方法,让我测试属性可以和不能被大量分配.老实说,我喜欢这种语法

it { should_not be_accessible :field_name }
it { should be_accessible :some_field }
Run Code Online (Sandbox Code Playgroud)

有没有人有更好的解决方案来解决这个问题?

rspec ruby-on-rails rspec2 rspec-rails

23
推荐指数
2
解决办法
5408
查看次数

无法使用rspec进行存根辅助方法

我试图在我的控制器中定义的助手上存根方法.例如:

class ApplicationController < ActionController::Base
  def current_user
    @current_user ||= authenticated_user_method
  end
  helper_method :current_user
end

module SomeHelper
  def do_something
    current_user.call_a_method
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的Rspec中:

describe SomeHelper
  it "why cant i stub a helper method?!" do
    helper.stub!(:current_user).and_return(@user)
    helper.respond_to?(:current_user).should be_true # Fails
    helper.do_something # Fails 'no method current_user'
  end
end
Run Code Online (Sandbox Code Playgroud)

spec/support/authentication.rb

module RspecAuthentication
  def sign_in(user)
    controller.stub!(:current_user).and_return(user)
    controller.stub!(:authenticate!).and_return(true)

    helper.stub(:current_user).and_return(user) if respond_to?(:helper)
  end
end

RSpec.configure do |config|
  config.include RspecAuthentication, :type => :controller
  config.include RspecAuthentication, :type => :view
  config.include RspecAuthentication, :type => :helper
end
Run Code Online (Sandbox Code Playgroud)

在这里 …

rspec ruby-on-rails rspec2 rspec-rails ruby-on-rails-3

22
推荐指数
4
解决办法
2万
查看次数

未初始化的常数Rspec

我创建了一个新的rails应用程序,并按照rspec-rails的安装说明进行操作 - https://github.com/rspec/rspec-rails 然后我创建(从interwebs中复制)我的app/lib目录中的以下模块.

require 'openssl'
require 'base64'

module Cipher
  def self.encrypt(key, data)
    data += 'A' # Add 'A' suffix to support empty data
    cipher(:encrypt, key, data)
  end

  def self.decrypt(key, text)
    data = cipher(:decrypt, key, text)
    data[0...-1] # Remove the 'A' suffix
  end

  def self.encrypt_base64(key, data)
    blowfish_string = self.encrypt(key, data)
    Base64.encode64(blowfish_string)
  end

  def self.decrypt_base64(key, base64_string)
    blowfish_string = Base64.decode64(base64_string)
    self.decrypt(key, blowfish_string)
  end

  private

  def self.cipher(mode, key, data)
    cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').send(mode)
    cipher.key = Digest::SHA256.digest(key)
    cipher.update(data) << cipher.final
  end
end
Run Code Online (Sandbox Code Playgroud)

然后我创建了以下spec文件.

require 'rails_helper' …
Run Code Online (Sandbox Code Playgroud)

rspec-rails

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

为sidekiq工作者编写测试

我正在使用rspec-sidekiqgem(https://github.com/philostler/rspec-sidekiq)帮助测试我正在编写的工作人员,但出于某种原因,我的测试仍然失败.

这是我的测试:

require 'spec_helper'

describe CommunicationWorker do
  it { should be_retryable false }

  it "enqueues a communication worker" do
    subject.perform("foo@bar.com", "bar@foo.com", [1,2,3])
    expect(CommunicationWorker).to have_enqueued_jobs(1)
  end
end
Run Code Online (Sandbox Code Playgroud)

这是错误:

 1) CommunicationWorker enqueues a communication worker
     Failure/Error: expect(CommunicationWorker).to have_enqueued_jobs(1)
       expected CommunicationWorker to have 1 enqueued job but got 0
     # ./spec/workers/communication_worker_spec.rb:9:in `block (2 levels) in <top (required)>'
Run Code Online (Sandbox Code Playgroud)

我将他们的低级别测试基于他们的维基上的示例,但它不适合我...任何理由为什么这不起作用?

rspec rspec-rails sidekiq rspec-sidekiq

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