我们的一些 Rails 路由需要区域设置来生成 url(例如domain.com/en/courses),而基本控制器(例如domain.com)不使用区域设置。default_url_options我们通过在右侧控制器中添加区域设置来设置它。
但是在 Rspec 中测试它时,我收到一个missing required keys错误,询问区域设置。当我在规范中添加区域设置参数时,visit course_path(@course, locale: :en)一切都恢复正常......但我们有很多规范!
我尝试使用default_url_option中的方法ActionView::TestCase::TestController,就像ActionDispatch::Routing::RouteSet这样
class ActionView::TestCase::TestController
def default_url_options(options = {})
{ locale: I18n.default_locale }
end
end
class ActionDispatch::Routing::RouteSet
def default_url_options(options = {})
{ locale: I18n.default_locale }
end
end
Run Code Online (Sandbox Code Playgroud)
但它设置了整个应用程序的区域设置,而不仅仅是一些特定的路线......
有没有办法在 rspec 支持中为某些路由设置默认参数?
假设我正在用 ruby 中的块参数调用 Ruby 方法:
Net::SFTP.start('testhost.com', 'test_user', keys: ['key']) do |sftp|
sftp.upload!('/local', '/remote')
end
Run Code Online (Sandbox Code Playgroud)
如何测试该upload!方法是否使用正确的参数调用?
我可以走到这一步,测试以下论点#start,
expect(Net::SFTP).
to receive(:start) do |host, username, keyword_args, &block|
expect(host).to eq("testhost.com")
expect(username).to eq("test_user")
expect(keyword_args).to eq(keys: ["test_key"])
end
Run Code Online (Sandbox Code Playgroud)
但我不知道如何测试#upload!块中调用的内容。
I\xe2\x80\x99m 使用 RSpec-rails 4.1、Rails 6.1 和 FactoryBot 6.2。运行 RSpec 示例时,我看到如下消息
\nDEPRECATION WARNING: This feature will be removed in the next major release.\nRun Code Online (Sandbox Code Playgroud)\n除上述信息外,没有其他信息。在我的 config/environments/test.rb 文件<我有这个设置
\nconfig.active_support.deprecation = :log\nRun Code Online (Sandbox Code Playgroud)\n但是,在我的 log/test.log 文件中,我没有看到一行 \xe2\x80\x9cDEPRECATION WARNING: This feature will be returned in the next main release\xe2\x80\x9d 或任何类似于警告堆栈跟踪的内容。如果我将上面的更改为
\nconfig.active_support.deprecation = :stderr\nRun Code Online (Sandbox Code Playgroud)\n我仍然看不到\xe2\x80\x99t 堆栈跟踪。如何获取有关导致弃用警告的精确行的更多信息?
\nrspec ruby-on-rails rspec-rails deprecation-warning ruby-on-rails-6
我正在开发 Apple M1 Pro,并遇到了 Docker 和 chromedriver 的几个问题。
\n所以我想在我的 Docker 容器上使用 selenium chrome headless 运行 RSpec 测试。我的Dockerfile是:
# Start from the official ruby image, then update and install JS & DB\nFROM --platform=linux/amd64 ruby:2.6.6\nRUN apt-get update -qq && apt-get install -y nodejs postgresql-client\n\n# Install Chromedriver\nRUN apt-get update && apt-get install -y unzip && \\\n CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \\\n wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/ && \\\n unzip ~/chromedriver_linux64.zip -d ~/ && \\\n rm ~/chromedriver_linux64.zip && \\\n chown …Run Code Online (Sandbox Code Playgroud) 我的项目开发的一部分已经完成.我们公司要求我为开发的代码和今后的开发编写黄瓜测试用例.routes文件有两个子域用于admin和hosts.Devise也在使用中.
现在我安装了黄瓜并为第一个故事写了第一个场景,当未注册的用户登陆主页,输入有效的电子邮件并被重定向到下一页......页面没有密码字段.
Scenario: Non registered user lands on beta home page.
Given: I am on the homepage
When: I enter valid email with "bahubalian...@gmail.com".
Then: I should be redirected to request invitation page.
Run Code Online (Sandbox Code Playgroud)
问题出在我的路线文件中,我有,
constraints :subdomain => ADMIN_SUBDOMAIN do
....
root :to => admin#index
end
constraints :subdomain => HOST do
...
root :to => home#index.
end
Run Code Online (Sandbox Code Playgroud)
现在,我如何指定path.rb文件以查找该特定子域中的root_path.没有root_path写在子域约束之外.这是我第一次参加测试.我真的很坚持这一点.非常感谢任何帮助.
我只是从某人那里得知,这可以用capybara来实现.如果是这样,你能不能对它有所了解.
对于给定的Rails(3)控制器,我有几种视图规格:编辑,索引,新建,显示,我希望在它们之间共享一些样板设置代码。我想避免将其放在spec_helper.rb文件中。有任何想法吗?
更具体地说,在spec / views / steps中,我有四个文件:{edit,new,show,index} .html.erb_spec.rb。我希望他们共享一些代码,例如
let(:workflow) do
document = Factory.create(:document)
document.user = user
document.save!
document.workflow
end
Run Code Online (Sandbox Code Playgroud)
例如-确切的代码无关紧要。我想这样做而不将其放在spec_helper.rb中。
我在轨道上使用带有红宝石的rspec进行测试.
问题 - 在规范中,如果我要进行交叉检查,在开始测试之前正确建立了前提条件,建议采用什么方法?
例如,使用rspec".should"类型断言似乎不正确,因为我只检查前提条件...
控制器测试中的rspec和rails有一个奇怪的问题.每当我们添加puts response一个规范时,它会输出很多这些
200
{"Content-Type"=>"text/html; charset=utf-8"}
200
{"Content-Type"=>"text/html; charset=utf-8"}
200
{"Content-Type"=>"text/html; charset=utf-8"}
200
{"Content-Type"=>"text/html; charset=utf-8"}
Run Code Online (Sandbox Code Playgroud)
然后失败了SystemStackError: stack level too deep.通过pry检查响应效果很好,打印其他东西也很好.
升级到最新的rspec(2.11)并没有什么不同.我们注意到puts调用to_a响应,它返回一个数组[@status, @header, self],所以它会以某种方式导致这种奇怪的递归?
更新:这是代码+规范的要点
我的观点快照:
<%= form_for @request do |f| %>
<div class="form-group">
<%= f.radio_button(:item, "Snow/waterproof shell (upper)") %>
<%= f.label(:item, "Snow/waterproof shell (upper)") %>
</br>
<%= f.radio_button(:item, "Headlamp") %>
<%= f.label(:item, "Headlamp") %>
</div>
Run Code Online (Sandbox Code Playgroud)
然而,在我的Rspec集成测试文件(spec/requests/requests_spec.rb)上,当我写的时(请注意,选择单选按钮是用户从列表中请求项目的表单的一部分,并且测试是针对结果页面的提交后,应指明用户请求的项目).我正在使用gem'rspec-rails','2.13.1'
describe "Requests" do
subject { page }
describe "new request" do
before { visit root_path }
describe "with valid information" do
before do
choose("Snow/waterproof shell (upper)")
click_button submit
end
it { should have_content("Snow/waterproof shell (upper)")
end
end
end
Run Code Online (Sandbox Code Playgroud)
我总是得到错误:
?[31mFailure/Error:?[0m ?[31mchoose("Snow/waterproof shell (upper)")?[0m
?[31mCapybara::ElementNotFound?[0m:
?[31mUnable to find radio …Run Code Online (Sandbox Code Playgroud) Rails 4.2.4,Rspec 3.3.1,shoulda-matcher 3.0.0.
我正进入(状态
#...
358) Participant validations
Failure/Error: it { should ensure_length_of(:coresp_country).is_at_most(255) }
NoMethodError:
undefined method `ensure_length_of' for #<RSpec::ExampleGroups::Participant::Validations:0x0000000f40aec0>
# ./spec/models/participant_spec.rb:100:in `block (3 levels) in <top (required)>'
359) Participant validations company
Failure/Error: it { should ensure_length_of(:company).is_at_most(255) }
NoMethodError:
undefined method `ensure_length_of' for #<RSpec::ExampleGroups::Participant::Validations::Company:0x0000000f414ab0>
# ./spec/models/participant_spec.rb:149:in `block (4 levels) in <top (required)>'
360) Participant validations company declared_type = COMPANY
Failure/Error: it { should validate_presence_of(:company) }
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::ExampleGroups::Participant::Validations::Company::DeclaredTypeCOMPANY:0x0000000f429c58>
#...
Run Code Online (Sandbox Code Playgroud)
还有更多此类故障(看起来像是不合适的).
rails_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', …Run Code Online (Sandbox Code Playgroud) rspec-rails ×10
rspec ×8
capybara ×3
ruby ×2
cucumber ×1
docker ×1
radio-button ×1
rspec2 ×1
selenium ×1
shoulda ×1