我在 Rails 3.2.11 应用程序中使用 Rspec。我已经安装并设置了 spork。\n我使用 datamapper 作为 ORM。
\n\n然而,当 spork 运行时,运行一个测试需要一分钟多的时间。\n即使正在运行的测试是空的。肯定有什么问题。但我无法弄清楚到底是什么。
\n\n我的 test_spec 是这样的:
\n\nrequire "spec_helper"\nrequire "cancan/matchers"\n\ndescribe User do\nend\nRun Code Online (Sandbox Code Playgroud)\n\n我的规范帮助文件可以在这里找到:https://gist.github.com/4593609
\n\n当我计时 rspec 时:
\n\n\xe2\x9e\x9c books git:(dev) \xe2\x9c\x97 time rspec --drb spec/models/test_spec.rb\nNo examples found.\n\n\nFinished in 1 minute 51.08 seconds\n0 examples, 0 failures\nrspec --drb spec/models/test_spec.rb 1.49s user 0.04s system 1% cpu 1:52.94 total\nRun Code Online (Sandbox Code Playgroud)\n\nSpork日志:
\n\n\xe2\x9e\x9c books git:(dev) \xe2\x9c\x97 spork\nUsing RSpec\nPreloading Rails environment\nLoading Spork.prefork block...\nRack::File headers parameter replaces cache_control after Rack 1.5.\nSpork is …Run Code Online (Sandbox Code Playgroud) 我有一个 Rails 应用程序 (3.2.12),我想通过 HTTP Accept-Language 标头添加区域设置切换。
我为实现这一目标所做的事情:
我将rack-contrib添加到我的Gemfile中:
gem 'rack-contrib', require: 'rack/contrib'
Run Code Online (Sandbox Code Playgroud)
ran bundle install,将中间件添加到 my config/application.rb:
config.middleware.use Rack::Locale
Run Code Online (Sandbox Code Playgroud)
并检查我的控制器的请求环境:
puts request.env.keys.select{|v| v=~/rack/ }
Run Code Online (Sandbox Code Playgroud)
我运行的规范是控制器规范,它包含render_views在其中。
我的问题:
rack.locale请求环境中没有键。我仔细检查了一下rake middlware,它列Rack::Locale在最后,就在之前run MyApp::Application.routes。
经过一些调试后,我发现运行时从未调用中间件
rspec spec/controllers/authentication_controller_spec.rb
Run Code Online (Sandbox Code Playgroud)
但是:运行相同的代码script/rails s thin可以在请求环境中提供更多键,即:
rack.request.cookie_string
rack.locale
rack.request.query_string
rack.request.query_hash
Run Code Online (Sandbox Code Playgroud)
所以,我想问题是:为什么 RSpec 拒绝选择 Rack 中间件?
我的 rails 控制器中有一个这个方法:
def some_init_func
# ...
@inst_var = 1
# ...
end
Run Code Online (Sandbox Code Playgroud)
后来:
do_something_with(@inst_var)
Run Code Online (Sandbox Code Playgroud)
如何在 RSpec 中设置这个实例变量?
allow(controller).to receive(:some_init_func) do
# ???? what goes here ????
end
Run Code Online (Sandbox Code Playgroud) 我有 rails 应用程序,并且在 application.js 文件中有一些 ajax 调用。O 有 rspec、capybara、webkit、factorygirl 和 databasecleaner 用于测试。我编写了全部通过的功能测试。然后不小心删除了 application.js 文件并运行测试,它们仍然通过。请帮我找出原因。非常感谢。
PS:如果您不赞成我的问题,请至少告诉我原因。我一直在寻找答案,但找不到。
我们有一个用 RSpec 编写的单元测试套件。我们有一些失败的测试,实际上数量很多。
我正在寻找的是一个脚本或魔术命令,将所有失败的测试标记为已跳过,这样我就不必一一检查它们并将它们标记为已跳过。
我有一个自定义 RSpec 匹配器,用于检查作业是否已安排。它的使用方式如下:
expect { subject }.to schedule_job(TestJob)
日程表作业.rb:
class ScheduleJob
include RSpec::Mocks::ExampleMethods
def initialize(job_class)
@job_class = job_class
end
...
def matches?(proc)
job = double
expect(job_class).to receive(:new).and_return job
expect(Delayed::Job).to receive(:enqueue).with(job)
proc.call
true
end
Run Code Online (Sandbox Code Playgroud)
这对于正匹配来说效果很好。但它不适用于负匹配。例如:
expect { subject }.not_to schedule_job(TestJob) #does not work
为了使上述工作正常进行,该方法需要在未满足期望时matches?返回。false问题是,即使它返回 false,无论如何都会创建期望,因此测试会错误地失败。
关于如何使这样的事情发挥作用有什么想法吗?
为了与 hashdata 进行比较,我们在规范中有这个
it 'should return the rec_1 in page format' do
expect(response_body_json).to eql(Preseneter.new(ActiveRecordObject).page)
end
Run Code Online (Sandbox Code Playgroud)
Presenter 是一个类,它将接受 ActiveRecordObject 并以特定格式响应哈希数据。
然后我们将带有时间戳的updated_at 添加到hash_data 中。
在我的代码中,我有updated_at = Time.zone.now
所以规范开始失败,因为两个 Updated_at 都有几秒钟的差异。
尝试存根时区
it 'should return the rec_1 in page format' do
allow(Time.zone).to receive(:now).and_return('hello')
expect(response_body_json).to eql(Preseneter.new(ActiveRecordObject).page)
end
Run Code Online (Sandbox Code Playgroud)
但现在response_body_json.updated_at作为“hello”出现,但右侧仍然带有时间戳
我哪里错了???或者还有其他更好的方法来处理这种情况吗?
由于 rspec 3.5 请求规范用于测试控制器行为以及路由测试以及视图与内容的正确呈现。最后一部分让我有点烦恼,因为我不明白视图规范中的内容与请求规范中的内容之间的细线。
在relishapp 上,我发现了这篇文章:
expect(response.body).to include("Widget was successfully created.")
这诱使我在请求测试中包含以下内容:
describe "Index page" do
....
it 'includes a link to cars_parts' do
get "/car_overview"
expect(response.body).to include("car_parts_path")
end
....
end
Run Code Online (Sandbox Code Playgroud)
此测试失败。在视图我使用link_to的 car_parts url_helper。失败的测试转储了整个 response.body 字符串,我看到了car_parts_path但 rspec 没有看到它。我如何更改我的测试以使其在不使用水豚的情况下通过,因为它现在只能用于功能规格。我到底做对了还是应该在其他地方进行这种测试?
我正在尝试使用水豚验证页面的响应代码。我使用了expect语句作为-
expect(page.status_code).to eq(404)
Run Code Online (Sandbox Code Playgroud)
我收到的错误是 -
Capybara::NotSupportedByDriverError:
Capybara::Driver::Base#status_code
Run Code Online (Sandbox Code Playgroud)
Capybara 可能不支持 status_code。有没有其他方法可以验证状态/响应代码,或者我在 expect 语句中做错了什么。
Rails 5.2 我有以下 ApplicationCable::Connection ruby 文件:
module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_user
def connect
self.current_user = find_verified_user
end
private
def find_verified_user
if verified_user = env['warden'].user
verified_user
else
message = "The user is not found. Connection rejected."
logger.add_tags 'ActionCable', message
self.transmit error: message
reject_unauthorized_connection
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想测试此设置并使用以下 RSpec 测试:
require 'rails_helper.rb'
RSpec.describe ApplicationCable::Connection, type: :channel do
it "successfully connects" do
connect "/cable", headers: { "X-USER-ID" => 325 }
expect(connection.user_id).to eq 325
end
end
Run Code Online (Sandbox Code Playgroud)
失败的原因是:
失败/错误:如果verified_user = env['warden'].user …
rspec-rails ×10
rspec ×7
ruby ×5
capybara ×2
rspec3 ×2
actioncable ×1
controller ×1
datamapper ×1
devise ×1
rack ×1
tdd ×1