我的中有以下内容rails_helper.rb:
RSpec.configure do |config|
# ...
config.before(:each, type: :controller) do
# SOMETHING
end
end
Run Code Online (Sandbox Code Playgroud)
我想定义SOMETHING适用的目录(在我的例子中仅适用于spec/controllers/api目录下的文件)。
有机会实现这一目标吗?
我正在尝试使规范适用于项目。有一个规格设置但没有维护。
我有一个简单的帖子request spec
require 'rails_helper'
RSpec.describe 'Cars API', type: :request do
let(:organization) { Fabricate(:organization, owner: user) }
let(:app) { Fabricate(:app, organization: organization, owner: user) }
let(:user) { Fabricate(:user) }
before do
#login_api(user)
end
describe 'create' do
describe 'car type' do
it 'should create a car of type CarType1' do
expect(Car.count).to eql(0)
id = '123'
post("/api/1/organization/#{id}/cars")
expect(Car.count).to eql(1)
car = Car.first
expect(car.type).to eql(Car::CarType1)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我得到
#<NoMethodError: undefined method `call' for #<App:0x007fee64557080>>
Run Code Online (Sandbox Code Playgroud)
我尝试调试该问题,但没有成功。问题发生在线路上post("/api/1/organization/#{id}/cars")。问题可能出在哪里?
我是 Rspec 的粉丝,但我从不使用控制器或视图规范。我知道存在以下选项:
Usage:
rails generate controller NAME [action action] [options]
Rspec options:
[--controller-specs], [--no-controller-specs] # Indicates when to generate controller specs
# Default: true
[--view-specs], [--no-view-specs] # Indicates when to generate view specs
# Default: true
Run Code Online (Sandbox Code Playgroud)
但是有没有办法让 Rspec 在默认情况下跳过生成这些?例如,我不想--no-controller-specs --no-view-specs每次生成控制器时都打字。
如何默认禁用规范生成?(同时保留一些其他的,例如模型)
我有一个使用rspec 3.4.0 capybara 2.6.2和的 rails 项目capybara-webkit 1.8.0
我有表单,其 html 如下:
<form class="jsForm padding " id="edit_seller_profile_20" enctype="multipart/form-data" action="/seller_profile" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="?"><input type="hidden" name="_method" value="patch"><input type="hidden" name="authenticity_token" value="RBjidiRHSDaPNRVmmfVBArIQL/MvjdWcm3897r5+gLuekmWtwROLhslAcVMgxyYde9EJ6KgsWdsgYxZvLqdC7g==">
<div class="row no-padding-bottom" id="first">
<!-- Popover START -->
<div class="helper">
<a tabindex="0" id="pop-trigger" role="button" data-toggle="popover" data-placement="bottom" data-trigger="focus" data-content="If you don't have a company logo, you could just put an image of yourself in here." data-original-title="" title="">
<div class="help-mark"></div>
</a>
</div>
<!-- Popover END -->
<div class="col-xs-12 no-margin-bottom profile-photo">
<p …Run Code Online (Sandbox Code Playgroud) 我有一个共享的上下文:
shared_context :current_country do
let!(:country) { create :country, slug: "kw" } unless Country.find_by(slug: "kw").present?
end
Run Code Online (Sandbox Code Playgroud)
我需要访问country每个规范中的变量。
要使用它,我需要include_context :current_country在每个规范文件中使用它。是否可以避免在每个规范文件中包含这一行,而是将上下文配置为在任何地方都可用?
我有一个测试,检查请求的页面是否返回 200 状态代码:
expect(response).to have_http_status(:success)
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用以下测试明确返回不同的状态代码:
return render json: { error: 'error message' }, status: :unprocessable_entity
Run Code Online (Sandbox Code Playgroud)
它仍然通过。
为什么response和last_response具有不同的状态:
response.status # 200
last_response.status # 422
Run Code Online (Sandbox Code Playgroud) 我正在处理一个遗留项目(最近从 rails 4.1 升级到 5.2),我不得不更改关联表。之前:报表有很多客户,客户有很多报表。现在我创建了一个ClientsReport表,它不仅包含 client_id 和 reports_id,而且还有一个idasprimary_key和一个can_manage(布尔值)。
与当调用rspec的它给我一个错误的测试reports_clients.for(report).first.can_manage
说unKnownAttribute can_manage的<ClientsReport client_id: 1, report_id:3> 任何迹象,如果id也没有can_manage
所以看起来它正在使用旧模式。
还尝试ActiveRecord::Migration.maintain_test_schema!按照此处的建议添加,但我不确定
我尝试运行,rake:db:prepare但它给我带来了一堆错误,现在看起来我破坏了测试数据库,因为我有 4 个失败的测试,现在我有 166 个......
我的 spec_helper.rb 看起来像这样:
Spork.prefork do
require 'simplecov'
SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'devise'
require './spec/controllers/controller_helpers.rb'
# Requires supporting …Run Code Online (Sandbox Code Playgroud) 既然 Rails 附带了凭证/秘密文件,我似乎无法使用 RSpec 存根/覆盖秘密。
# credentials.yml.enc
my_token: 111
Run Code Online (Sandbox Code Playgroud)
使用环境变量,我们曾经这样做过
allow(ENV).to receive(:[]).with('my_token').and_return('')
Run Code Online (Sandbox Code Playgroud)
所以我期望能够应用相同的逻辑
allow(Rails.application.credentials).to receive(:my_token).and_return('')
Run Code Online (Sandbox Code Playgroud)
但它不会覆盖 Rails 的秘密。任何想法?谢谢
我有以下工厂
FactoryBot.define do
factory :function_group do
factory :reports do
# id 4
name { "Reports" }
functions {
[find_or_create(:organization_report_function),
find_or_create(:account_status_report_function)]
}
end
end
end
Run Code Online (Sandbox Code Playgroud)
FactoryBot.define do
factory :function do
factory :organization_report_function do
name { "Org Report Function" }
module_class { "organization_report_function" }
end
factory :account_status_report_function do
name { "Acct Status Report" }
module_class { "account_status_report_function" }
end
end
end
Run Code Online (Sandbox Code Playgroud)
在我的 rails_helper.rb 中,我的代码如下所示
require 'email_spec'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'capybara/poltergeist'
SERVER_PORT = 10000 + ENV.fetch('TEST_ENV_NUMBER', …Run Code Online (Sandbox Code Playgroud) 安装 Ruby 2.7.0 后,运行规范已成为诸如此类警告的噩梦:
/Users/user/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/capybara-3.30.0/lib/capybara/node/matchers.rb:835: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/user/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/capybara-3.30.0/lib/capybara/node/matchers.rb:861: warning: The called method `_set_query_session_options' is defined here
Run Code Online (Sandbox Code Playgroud)
由于这些警告是从 gem 中弹出的,它们确实没有帮助,并且使 RSpec 输出变得一团糟。
我曾尝试将此行添加到 spec_helper.rb
config.warnings = false
Run Code Online (Sandbox Code Playgroud)
这行到 config/environments/test.rb
config.active_support.deprecation = :log
Run Code Online (Sandbox Code Playgroud)
但是,仍然有数百个警告弹出。我能做些什么来摆脱它们吗?
运行 Ruby on Rails 6.0.2.1 和 Ruby 2.7.0
注意 有人建议在运行规范时抑制 Ruby 警告中已经存在这个问题的答案这看起来像一个类似的问题,但提供的解决方案没有效果。我看到了所有的警告。