Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the describe text in rspec

Tags:

ruby

rspec

I'm writing some specs that test the template files in a gem that has generators for Rails. I'd love to access to "admin_layout.html.erb" in the rspec spec 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
like image 717
Josh Crews Avatar asked Jan 05 '12 15:01

Josh Crews


1 Answers

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

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

However, keep in mind this will only put out the first parent's description. So if you have contexts in your describe block, then the examples within the contexts would give the context name for self.class instead of the describe block's name. In that case, you could 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

In case you want the 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
like image 56
Dylan Markow Avatar answered Sep 23 '22 07:09

Dylan Markow