标签: rspec-rails

为什么Rails测试环境中的机架env哈希是空的?

在我的Rails应用程序中,我正在访问我的一个控制器操作中的env哈希.

有点像:

def my_before_filter
  env['some.key'] = "Something or other"
end
Run Code Online (Sandbox Code Playgroud)

这对我的要求非常有用.

如果我在测试环境中启动我的Rails应用程序,并访问如下操作:

# /users in UsersController#index
def index
  puts env.inspect
end
Run Code Online (Sandbox Code Playgroud)

然后,env哈希的内容按预期输出到控制台.

当我从RSPec示例中获取此操作时,输出是空哈希?

it 'should get the index action' do
  get :index
end

.....{}.... # rspec output
Run Code Online (Sandbox Code Playgroud)

为什么env哈希是空的?

我已经构建了一个虚拟轨道应用来展示这一点

ruby rack ruby-on-rails rspec-rails

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

在Windows上使用Guard和rspec更改文件时,测试不会运行

我已经安装了guard-rspec gem用于我的rails应用程序.当我从命令行启动后bundle exec guard,它第一次运行我的整个测试套件没有问题.

但是,每当我对我的Guardfile中指定的spec文件或任何监视文件进行任何更改时,Guard似乎都无法识别这些更改,也不会重新运行任何测试.

我甚至尝试在我的Guardfile中放置一些显式内容,如下所示:

watch("app/views/orders/new.html.erb")          { "spec/requests/orders_spec.rb" }
Run Code Online (Sandbox Code Playgroud)

orders_spec.rb当我编辑并保存new.html.erb正确时应该触发我的测试?好吧,当我编辑它并点击保存时,没有任何反应,甚至没有错误或警告.

有没有人有幸在Windows上运行Guard或遇到类似的问题?

rspec ruby-on-rails guard rspec-rails

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

Capybara + RSpec,spec/features dir被rspec忽略.命令?

我使用的是新水豚DSL使用RSpec以下位于短方针这里 并添加到测试spec/features目录中运行以及单独的,即.

rails_project$ rspec spec/features/my_first_feature.rb

然而,spec/features目录完全忽略了,当我尝试运行整个测试spec/这样的目录:

rails_project$ rspec .

指南中没有提到如何包含这个目录(或其他目录)来完成我想要的包含.我真的需要这个来持续集成我的项目.

有人可以告诉我怎么做吗?

谢谢!

ruby testing rspec capybara rspec-rails

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

Rspec在Rails 4 beta中没有生成*_spec.rb文件

我已经安装了rspec-rails gem.当我跑步时,rails g model movie showtime_date:date showtime_time:time我想,我应该得到

invoke  active_record
      create    db/migrate/20130604010556_create_movies.rb
      create    app/models/movie.rb
      invoke    rspec
      create    spec/models/movie_spec.rb
Run Code Online (Sandbox Code Playgroud)

但是当我跑步时rails g model movie showtime_date:date showtime_time:time 我得到了

invoke  active_record
      create    db/migrate/xxxxxxxxxxx_create_movies.rb
      create    app/models/movie.rb
      invoke    test_unit
      create      test/models/movie_test.rb
      create      test/fixtures/movies.yml
Run Code Online (Sandbox Code Playgroud)

我的gem包有问题吗?我正在使用Rails4 beta.这个版本有错误还是其他什么?

我的应用程序的BDD是这样的 app/features/show_descripitons.feature

Feature: Showtime Descriptions
    As a movie goer
    I want to see accurate and concise showtimes
    So that I can find movies that fit my schedule

    @wip
    Scenario: Show minutes for times ending with 00
        Given a movie …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails cucumber rspec-rails ruby-on-rails-4

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

运行rspec时隐藏文件列表?

rake spec在大型Rails项目的命令行上运行时,我会得到一个将运行的每个rspec文件的巨大列表.

有默认隐藏的方法吗?

ruby-1.9.3-p448/bin/ruby -S rspec ./spec/acceptance/replicators/activity_replicator_spec.rb ./spec/acceptance/replicators/template_replicator_spec.rb ./spec/authorization_rules/admin_authorization_rules_spec.rb ...
Run Code Online (Sandbox Code Playgroud)

当我只运行rspec(没有拨打电话)我没有得到这个控制台输出.


编辑1

根据phoet的回答,我试过了

RSpec::Core::RakeTask.new(:spec) do |t|
  t.verbose = false
  t.warning = false
  t.rcov = false
end

task :default => :spec
Run Code Online (Sandbox Code Playgroud)

这并没有解决问题.

rake configuration rspec2 rspec-rails ruby-on-rails-3

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

使用rails 4.1运行rspec时出现ActiveRecord :: AdapterNotSpecified错误

我无法在Google或stackoverflow上的任何其他位置找到此确切错误.

我正在运行Rails 4.1.0和rspec-rails 3.0.0.beta2

尝试运行$ rspec时,出现以下错误:

    DEPRECATION WARNING: Passing a string to ActiveRecord::Base.establish_connection for a configuration lookup is deprecated, please pass a symbol (:development) instead. (called from <top (required)> at /Users/sam/code/rails/innovacert/spec/spec_helper.rb:4)
/Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:257:in `resolve_symbol_connection': 'development' database is not configured. Available: ["test"] (ActiveRecord::AdapterNotSpecified)
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:240:in `resolve_string_connection'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:267:in `resolve_hash_connection'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:228:in `resolve_connection'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:152:in `resolve'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:164:in `block in resolve_all'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:163:in `each'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/connection_specification.rb:163:in `resolve_all'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_handling.rb:69:in `resolve'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/core.rb:46:in `configurations='
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/core.rb:48:in `block in <module:Core>'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/concern.rb:120:in `class_eval'
    from /Users/sam/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/concern.rb:120:in …
Run Code Online (Sandbox Code Playgroud)

rspec-rails rails-activerecord ruby-on-rails-4.1

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

如何在Rails控制器动作的RSpec测试中指定查询字符串?

我有一个叫做的动作yearly_csv.在这个动作中,我执行两个操作,如需求和供应.

def yearly_csv
   if demand == 'true'
       demand_csv
   else
       supply_csv
   end
end
Run Code Online (Sandbox Code Playgroud)

我在视图中有两个单选按钮来选择其中一个操作.现在我想在RSpec中单独测试每个操作.例如,一个供应规格和另一个需求规格.我的问题是如何将单选按钮值传递给yearly_csv动作(get)?

ruby rspec ruby-on-rails capybara rspec-rails

7
推荐指数
2
解决办法
7025
查看次数

`method_missing':`build`在示例组中不可用(例如`describe`或`context`块)

我想删除FactoryGirl.build(:user)每次我想创建一个用户,所以我添加了这些行:

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
end
Run Code Online (Sandbox Code Playgroud)

到了spec_helper.rb.但是这会产生以下错误:

`method_missing': `build` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). (RSpec::Core::ExampleGroup::WrongScopeError)
Run Code Online (Sandbox Code Playgroud)

然后我删除了所有上下文/描述块,但这并没有改变任何东西.你有没有遇到同样的问题,我该如何解决?

目前我的测试看起来像这样:

require 'rails_helper'

RSpec.describe User, type: :model do
  user = build(:user)
  project = build(:project)

  it "is valid with a firstname, lastname, email and password" do …
Run Code Online (Sandbox Code Playgroud)

testing unit-testing rspec ruby-on-rails rspec-rails

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

如何在ApplicationRecord抽象基类中测试方法?

我还没有找到测试ApplicationRecord方法的好方法。

假设我有一个简单的方法,名为one

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  def one
    1
  end
end
Run Code Online (Sandbox Code Playgroud)

我想测试一下:

describe ApplicationRecord do
  let(:it) { described_class.new }

  it 'works' do
    expect(it.one).to eq 1
  end
end
Run Code Online (Sandbox Code Playgroud)

这毫不奇怪地死于 NotImplementedError: ApplicationRecord is an abstract class and cannot be instantiated.

所以我在Rspec中的测试抽象类中尝试了匿名类建议:

let(:it) { Class.new(described_class).new }
Run Code Online (Sandbox Code Playgroud)

并且此操作死于TypeError: no implicit conversion of nil into String,大概是因为记录的表名是nil。

谁能建议一种很好的简单方法来测试ApplicationRecord方法?希望它不会在我的应用程序中引入对其他类的依赖关系,并且不会扎根于ActiveRecord内部吗?

rspec rspec-rails rails-activerecord ruby-on-rails-5

7
推荐指数
2
解决办法
1606
查看次数

如何运行多个特定的RSpec测试?

我有一个巨大的测试套件(> 5000次测试)需要一个小时才能运行.400失败了,我有一个失败的测试列表,如下所示:

rspec ./spec/models/fullscreen_hidden_view_state_spec.rb:116 # FullscreenHiddenViewState showing a playlist on third element - slide has all the right links and properties
rspec ./spec/features/fullscreen/play_spec.rb:59 # View case in fullscreen presention mode Courtesy section when viewing the cases discussion via the cases hidden share token the Add To button should be hidden
rspec ./spec/features/cases/index_spec.rb:204 # finding a case Filtering by study modality Filtering by modality only shows cases with modality
rspec ./spec/models/playlist_spec.rb[1:2:2:2:1] # Playlist it should behave like an entity with privacy …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails rspec-rails

7
推荐指数
2
解决办法
2712
查看次数