我最近经常使用 Rails I18n.yml文件,我发现当它们嵌套得很深时,导航起来相当乏味。例如,当我必须添加“en.this.is.some.deeply.nested.value”的翻译并且我不知道相应的结构是否已经存在(以及达到哪个级别)时,我必须做一个大量的手动浏览和搜索。我严重缺少一个有效的“转到符号”对话框 ( Cmd-Shift-T),该对话框可用于许多其他文件类型,但不适用于 yml (它只是空的)。那么也许有一个 YML 包支持这个?我已经使用https://github.com/textmate/yaml.tmbundle更新了我的包,但这没有此功能。
我正在努力获得一个很好的Ruby编码风格.为了防止意外调用具有相同名称的局部变量,我总是self.在适当的地方使用.但现在我偶然发现了这个:
class MyClass < ActiveRecord::Base
before_validation :sanitize_user_data
private
def sanitize_user_data
self.sanitize_name # with ".self" it's a problem, without it's not!
end
def sanitize_name
unless self.name.nil?
self.name.gsub!(/\s+/, ' ')
self.name.strip!
end
end
end
Run Code Online (Sandbox Code Playgroud)
上面的代码导致错误
私有方法
sanitize_name叫
但是当删除self.和使用时sanitize_name,它的工作原理.为什么?
我有一个帮助访问request.fullpath.在隔离的辅助测试中,request不可用.我该怎么办?我能以某种方式嘲笑它或类似的东西吗?
我正在使用最新版本的Rails和RSpec.这是我的助手的样子:
def item(*args, &block)
# some code
if request.fullpath == 'some-path'
# do some stuff
end
end
Run Code Online (Sandbox Code Playgroud)
因此,有问题的代码行是#4,其中帮助程序需要访问request辅助规范中不可用的对象.
非常感谢您的帮助.
我刚刚阅读了Rails的concat方法来清理输出内容的助手http://thepugautomatic.com/2013/06/helpers/.
我玩弄了它,我发现它对于带有花括号的块和带有do ... end的块没有同样的反应.
def output_something
concat content_tag :strong { "hello" } # works
concat content_tag :strong do "hello" end # doesn't work
concat(content_tag :strong do "hello" end) # works, but doesn't make much sense to use with multi line blocks
end
Run Code Online (Sandbox Code Playgroud)
我不知道花括号和做...结束块似乎有不同的含义.有没有办法使用concatdo ... end 而不用括号括起来(第3个例子)?否则它在某些情况下似乎没用,例如当我想在其中连接UL时,其中包含许多LI元素,因此我必须使用多行代码.
我使用RSpec设置了一个新的Rails 4应用程序.但是在运行时rake rspec,所有示例都运行了两次:
rake spec
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
/Users/josh/.rvm/rubies/ruby-2.0.0-p353/bin/ruby -S rspec ./spec/controllers/dashboards_controller_spec.rb ./spec/models/member_spec.rb ./spec/requests/members_spec.rb ./spec/routing/members_routing_spec.rb
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
11/11 |============================================ 100 ============================================>| Time: 00:00:00
Finished in …Run Code Online (Sandbox Code Playgroud) I have added visually hidden jump links to my website, that appear when they are focused (similar to e.g. GitHub, just press Tab once on the home page).
我想用 Capybara 测试这种行为。我无法检查例如“可见性:真/假”,因为链接并未真正隐藏(否则屏幕阅读器将看不到它们),而只能通过绝对定位从视口移出。当它们被聚焦时,它们被放置在视口中的原始位置。
所以我想我必须检查 X 和 Y 坐标,但 Capybara 似乎没有提供获取它们的本地方法?这是真的?如果是这样,我将如何为例如元素“#jump_to_content”获取它们?通过执行一些 JavaScript?
更新
我在这里找到了一些灵感:https : //content.pivotal.io/blog/testing-accessibility-with-rspec-and-capybara
但这似乎不适用于我的配置:
link.native.location
NoMethodError: undefined method `location' for #<Capybara::Poltergeist::Node tag="a">
Run Code Online (Sandbox Code Playgroud) 我有一个ReportsController嵌套ProjectsController的#show方法:
def show
# Some stuff
do_something(@report)
end
Run Code Online (Sandbox Code Playgroud)
路线:
resources :projects do
resources :reports
end
Run Code Online (Sandbox Code Playgroud)
我需要测试该do_something方法是否被调用:
it 'calls do_something' do
expect(controller).to receive(:do_something)
project = create :project
report = create :report, project: project
get :show, params: {project_id: project.id, id: report.id}
end
Run Code Online (Sandbox Code Playgroud)
我放置binding.pry在#show动作中,但这不会被调用.那么我的规格有什么问题?
我在这样的验证中测试了很多坏字符串:
['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm', 'h2m', 'hm2', '2m10h', '2m10m', '2h10h'].each do |input|
FactoryGirl.build(:time_record, duration_string: input).should have(1).error_on('duration_string'), "Tested value was '#{input}'"
end
Run Code Online (Sandbox Code Playgroud)
可悲的是,当其中一个失败时,消息expected 1 error on :duration_string, got 0并没有告诉我,哪一个失败了.我知道我可以传递显示的第二个参数而不是默认消息,如下所示:
x.should have(1).error_on('duration_string'), "Tested value was '#{input}'"
Run Code Online (Sandbox Code Playgroud)
但这隐藏了原始信息.有没有办法只将自己的信息附加到原始信息而不是替换它?
谢谢.
我有这个方法ApplicationHelper:
def home_link_class
classes = ['navbar-brand']
classes << 'active' if request.path == root_path
classes
end
Run Code Online (Sandbox Code Playgroud)
我想像这样测试它:
describe '#home_link_class' do
before { allow(helper.request).to receive(:path).and_return '/some-path' }
subject { home_link_class }
it { should eq ['navbar-brand'] }
end
Run Code Online (Sandbox Code Playgroud)
遗憾的是,存根似乎不起作用,request帮助器本身的对象被设置为nil,即使在规范中它似乎是一个ActionController::TestRequest对象.
如何确保request规格中提供?
在 Material UI 中,有一个自动完成功能。当我聚焦输入并按向上/向下时,会显示选项。
我想使用右键单击 -> 检查(在 Chrome 中)来检查这些选项。但是一旦焦点离开输入,选项就会消失(我在 DOM 中找不到它们),可能是因为一个onBlur或类似的 JavaScript 事件。
https://material-ui.com/components/autocomplete/
有没有办法让选项保持可见,即使焦点离开输入,这样我就可以检查它们的 DOM?
rspec ×4
javascript ×2
block ×1
bundle ×1
concat ×1
controller ×1
nested ×1
onblur ×1
private ×1
request ×1
rspec-rails ×1
ruby ×1
testing ×1
textmate ×1
yaml ×1