我正在编写一些规范来测试具有Rails生成器的gem中的模板文件.我想在下面的rspec规范中访问“admin_layout.html.erb”:
require 'spec_helper' describe "admin_layout.html.erb" do it "has page title Admin" do HERES WHERE I WOULD LOVE TO HAVE ACCESS TO "admin_layout.html.erb" AS A VARIABLE end end
解决方法
您可以使用self.class.description获取此信息:
it "has page title Admin" do layout = self.class.description # => "admin_layout.html.erb" end
但是,请记住,这只会显示第一个父母的描述.因此,如果您在describe块中有上下文,那么上下文中的示例将为self.class而不是describe块的名称提供上下文名称.在这种情况下,您可以使用元数据:
describe "admin_layout.html.erb",:layout => "admin_layout.html.erb" context "foo" do it "has page title Admin" do layout = example.Metadata[:layout] end end end