简体   繁体   English

如何使用RSpec测试Rails引擎gem的视图局部或辅助方法?

[英]How to test a view partial or helper method for a Rails engine gem using RSpec?

I've spent the last 2 days reading everything I could find on blogs, in books, in other gems' source, and on SO and I can't for the life of me figure this out. 我花了最后两天的时间阅读所有可以在博客,书籍,其他gems来源以及SO上找到的所有内容,我一生都无法理解。 Admittedly I'm new to writing Ruby gems, Rails engines, and am not entirely familiar with all the RSpec view methods (but I'm learning as much as I can access). 诚然,我是刚开始编写Ruby gems,Rails引擎,并且并不完全熟悉所有RSpec视图方法(但是我正在学习尽可能多的知识)。

I've made my project public on Github (albeit in the early stages, but this is based on some markup I've been reusing on recent projects that I'm slowly trying to pull into this gem) here: 我已经在Github上公开了我的项目(尽管处于早期阶段,但这是基于我一直在重用最近正在慢慢尝试融入这个瑰宝的最新项目中使用的一些标记):

https://github.com/robmclarty/admin_bar-rails https://github.com/robmclarty/admin_bar-rails

I've stopped adding new features while I figure out how to get my tests working. 在我弄清楚如何使测试正常工作的同时,我已停止添加新功能。 The main issue is a test I'm trying to run that simply renders a view partial (something I can build on to check other stuff later) like this: 主要问题是我正在尝试运行的测试,该测试仅使视图部分呈现(以后可以在此基础上检查其他内容),如下所示:

render :partial => 'admin_bar-rails/bar.html.haml'
rendered.should contain 'admin-panel'

...but I get a NameError from RSpec regarding view_renderer as follows: ...但是我从RSpec收到一个有关view_rendererNameError ,如下所示:

Failures:

  1) rendering admin bar should render an empty admin bar
     Failure/Error: render :partial => 'admin_bar-rails/bar.html.haml'
     NameError:
       undefined local variable or method `view_renderer' for #<RSpec::Core::ExampleGroup::Nested_2:0x007f99f260ec88>
     # ./spec/views/admin_bar-rails/bar.html.haml_spec.rb:13:in `block (2 levels) in <top (required)>'

I've tried including and requiring all kinds of stuff that I've read about (as you can see from the commented lines in my code), but I can't get it to pass. 我已经尝试过并要求包含我已阅读的各种内容(如您在代码中的注释行中所见),但是我无法通过它。

This is mostly an "insert dynamic markup into views" gem so I (and you) can reuse some common templates in other Rails projects. 这主要是“在视图中插入动态标记”的工具,因此我(和您)可以在其他Rails项目中重用一些通用模板。 Am I just going about this wrong? 我只是要解决这个问题吗? Do people even test views like this inside gems? 人们甚至在宝石中测试这种视图吗? Can anyone point me to some other gem examples that are more view/asset-heavy that use RSpec? 谁能指出我使用RSpec的其他一些更多视图/资产繁重的gem示例?

Ended up taking an alternate path by compiling the markup that I previously had in a view partial and creating the same thing through content_tag calls in a helper method. 最终通过编译我先前在局部视图中使用的标记并通过helper方法中的content_tag调用创建了相同的东西而走了一条替代路径。 This has a couple of benefits: 1) keeps all the relevant code in one place, 2) makes the parts easier to test, and 3) simplifies the gem because now I don't need any views at all (just Ruby/Rails code). 这有两个好处:1)将所有相关代码放在一个位置,2)使部件更易于测试,以及3)简化了gem,因为现在我根本不需要任何视图(只需Ruby / Rails代码) )。

Note: I removed the old gem I linked to in my question and replaced it with the following: 注意:我删除了我在问题中链接到的旧宝石,并用以下内容替换了它:

http://github.com/robmclarty/admin_nav-rails http://github.com/robmclarty/admin_nav-rails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM