How to access text description in rspec

I am writing some specifications that check template files in gem with generators for Rails. I would like to access "admin_layout.html.erb" in the rspec specification below:

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 
+7
source share
1 answer

You can use self.class.description to get this information:

 it "has page title Admin" do layout = self.class.description # => "admin_layout.html.erb" end 

However, keep in mind that this will only issue the first parent description. Therefore, if you have contexts in your describe block, then the examples in the contexts will give the context name for self.class instead of the name of the describe block. In this case, you can use metadata:

 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 

If you need a top-level description, you can use self.class.top_level_description :

 RSpec.describe "Foo", type: :model do context "bar" do it "is part of Foo" do self.class.top_level_description # => "Foo" end end end 
+14
source

Source: https://habr.com/ru/post/905205/


All Articles