将Rails应用程序升级到Rails 5后,运行RSpec测试会出现以下错误:
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError:
Environment data not found in the schema. To resolve this issue, run:
bin/rails db:environment:set RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)
但是,这bin不存在,我似乎无法使用bundle binstubs rails或生成它rake rails:update:bin.
我也尝试过:
rails db:environment:set RAILS_ENV=test
rake db:environment:set RAILS_ENV=test
Run Code Online (Sandbox Code Playgroud)
有在Github上一个相关的问题在这里.
我该如何解决这个错误?
我将我升级rspec-rails到3.0.1,现在我在所有测试中都看到了这个错误
Failure/Error: Sidekiq::Status::complete?(json.jid).should be_true
expected true to respond to `true?`
Run Code Online (Sandbox Code Playgroud)
我找不到解决方案,也找不到我所缺少的.
为了确保我的应用程序不容易受到这种攻击,我试图在RSpec中创建一个控制器测试来覆盖它.为了做到这一点,我需要能够发布原始JSON,但我似乎没有找到一种方法来做到这一点.在进行一些研究时,我已经确定使用RAW_POST_DATA标题至少有一种方法可以这样做,但这似乎不再起作用了:
it "should not be exploitable by using an integer token value" do
request.env["CONTENT_TYPE"] = "application/json"
request.env["RAW_POST_DATA"] = { token: 0 }.to_json
post :reset_password
end
Run Code Online (Sandbox Code Playgroud)
当我查看params散列时,令牌根本没有设置,它只包含{ "controller" => "user", "action" => "reset_password" }.我在尝试使用XML时得到了相同的结果,或者甚至在尝试使用常规的帖子数据时,在所有情况下,它似乎都没有设置它的周期.
我知道,随着最近的Rails漏洞,参数被散列的方式发生了变化,但仍有办法通过RSpec发布原始数据吗?我可以以某种方式直接使用Rack::Test::Methods?
我正在使用Rails 5和Devise 3.5.1.
阅读一本关于创建/测试API的好(旧)书,该书使用Devise身份验证.它是在Rails 5之前编写的,所以我选择不使用新的api-only版本.
这是我的测试......
#/spec/controllers/api/v1/users_controller_spec.rb
require 'rails_helper'
describe Api::V1::UsersController, :type => :controller do
before(:each) { request.headers['Accept'] = "application/vnd.marketplace.v1" }
describe "GET #show" do
before(:each) do
@user = FactoryGirl.create :user
get :show, params: {id: @user.id}, format: :json
end
it "returns the information about a reporter on a hash" do
user_response = JSON.parse(response.body, symbolize_names: true)
expect(user_response[:email]).to eql @user.email
end
it { should respond_with 200 }
end
end
Run Code Online (Sandbox Code Playgroud)
这是一个完全出乎意料的RSpec错误
Devise::MissingWarden:
Devise could not find the `Warden::Proxy` instance on your request environment. …Run Code Online (Sandbox Code Playgroud) 当我向他们添加":js => true"时,我在许多测试中遇到了类似的错误.例如:
An error occurred in an after hook
ActionController::RoutingError: No route matches [GET] "/assets"
occurred at /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
Run Code Online (Sandbox Code Playgroud)
我以前没有在我的应用程序中测试启用JavaScript的东西,只是通过升级到Capybara 2并安装Database Cleaner来设置这样做.config.use_transactional_fixtures = false,我在我的spec_helper文件中添加了一些前/后钩子(钩子?),我直接从这里接受的答案中复制了.
我在跑:
任何人都可以告诉我如何攻击这个?非常感谢!
4) Event pages 'CREATE' submitting a valid form provides a success notification and displays new event's page
Failure/Error: Unable to find matching line from backtrace
ActionController::RoutingError:
No route matches [GET] "/assets"
# /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
# /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
# /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/railties-3.2.5/lib/rails/rack/logger.rb:26:in `call_app'
# /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/railties-3.2.5/lib/rails/rack/logger.rb:16:in `call'
# /Users/appletart/.rvm/gems/ruby-1.9.3-p0@eventful2/gems/actionpack-3.2.5/lib/action_dispatch/middleware/request_id.rb:22:in …Run Code Online (Sandbox Code Playgroud) 不确定这是否是Capybara问题,但我刚刚安装了Rails(4.1.1)和Rspec Rails(3.0.1),我想使用Capybara(2.3.0),但从未使用它我'遇到问题.我在spec/features中创建了一个"spec",规范本身按预期运行并传递,但是当运行rspec时,我得到以下"警告".有任何想法吗?我只需要在红宝石级别禁用警告吗?
/Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/mail-2.5.4/lib/mail/network/delivery_methods/sendmail.rb:53: warning: shadowing outer local variable - to
/Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247: warning: loading in progress, circular require considered harmful - /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/capybara-2.3.0/lib/capybara.rb
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/bin/ruby_executable_hooks:15:in `<main>'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/bin/ruby_executable_hooks:15:in `eval'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/bin/rspec:23:in `<main>'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/bin/rspec:23:in `load'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/exe/rspec:4:in `<top (required)>'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:70:in `run'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:85:in `run'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/runner.rb:97:in `setup'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in `load_spec_files'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in `each'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in `block in load_spec_files'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/rspec-core-3.0.0/lib/rspec/core/configuration.rb:1051:in `load'
from /Users/typeoneerror/Dev/Doki/engines/doki_core/spec/features/accounts/sign_up_spec.rb:1:in `<top (required)>'
from /Users/typeoneerror/Dev/Doki/engines/doki_core/spec/features/accounts/sign_up_spec.rb:1:in `require'
from /Users/typeoneerror/Dev/Doki/engines/doki_core/spec/rails_helper.rb:7:in `<top (required)>'
from /Users/typeoneerror/.rvm/gems/ruby-2.1.2@doki/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in …Run Code Online (Sandbox Code Playgroud) 所以Rails 3.1附带了一个鲜为人知的"rails g plugin new"生成器,它为你提供了一个适合rails gem插件的骨架.[http://guides.rubyonrails.org/plugins.html#or-generate-a-gemified-plugin]
这样做的一个有用的事情是为Test :: Unit的测试设置方便.它为您提供了一个基本的虚拟Rails应用程序,您的测试可以在上下文中运行,以测试仅在Rails应用程序副本中运行的"引擎"行为.(它把它放在./test/dummy中).但是你的测试仍然在my_gem/test中,测试不会存在于虚拟app中.并且my_gem/test/test_helper.rb在那里编写,测试将在虚拟应用程序的上下文中运行,在../dummy/config/environment上启动.
我之所以这样描述是因为我认为很多人都不知道这个新的发电机,它可以很好地设置.
但我的问题是,有没有人想出如何用rspec做这个呢?我曾尝试按照相同的原则DIY为rails specing中的rspec设置这样的东西,但我遇到了各种令人困惑的障碍,我希望也许别人已经弄明白了(或者有兴趣搞定它对我们其他人来说,嘿).
我的ActiveRecord中有以下验证.
validates :active, :inclusion => {:in => ['Y', 'N']}
Run Code Online (Sandbox Code Playgroud)
我使用以下来测试我的模型验证.
should_not allow_value('A').for(:active)
should allow_value('Y').for(:active)
should allow_value('N').for(:active)
Run Code Online (Sandbox Code Playgroud)
是否有更清洁,更通过测试方式?我目前正在使用RSpec2和shoulda匹配器.
编辑
经过一些环顾四周我才发现,这可能是一种'好'的测试方式,如果没有为此提供任何东西,任何需要它的人都可以为它编写自己的自定义匹配器.(并可能将其贡献给项目) .可能有趣的讨论的一些链接:
我开始制作Rails 3.1引擎,我很难用rspec测试它.
首先,如果我运行rails g integration_test whatever它会在测试/集成中创建一个常规的集成测试而不是spec/requests(rspec-rails gem被安装并作为gemspec文件中的开发依赖项而需要)
此外,当我运行规范测试时,我得到一个错误,说明与我正在测试的模型相对应的表尚未创建.我尝试rake engine_name:install:migrations并rake db:migrate从虚拟应用程序内部运行,我得到一个"表已存在"错误.
一切似乎都断开了,我觉得我在这里遗漏了一些东西,使rspec gem无缝地工作,就像通常使用完整的rails应用程序一样.
我从http://rubyx.com/2011/03/01/start-your-engines跟踪了所有更改,我可以通过控制台启动虚拟应用程序手动测试引擎,如http:// railscasts所示. com/episodes/277-mountable-engines.
有没有办法让rspec成为测试rails 3.1引擎的默认设置?
rspec ruby-on-rails rails-engines rspec-rails ruby-on-rails-3.1
我想知道before(如下所示)是否与before :allRSpec 相同.有时既:each没有:all指定也没有指定,它使我对before实际做的事感到困惑.
require 'spec_helper'
describe "this is a description" do
before do # vs. before :all or before :each
# do something...
end
end
Run Code Online (Sandbox Code Playgroud)
如果有人可以解释差异,如果有的话,将不胜感激.谢谢!