Odd behavior of example metadata

32 views
Skip to first unread message

br...@targetedvictory.com

unread,
Feb 28, 2015, 2:48:01 PM2/28/15
to rs...@googlegroups.com
Consider this (simplified) snippet. RSpec 3.1.7

RSpec.describe MyController, :type => :controller do

  describe
"PUT #update" do

    before
(:example) do
      puts
self.class                          #=> RSpec::ExampleGroups::MyController::PUTUpdate
      puts
self.class.example.description      #=> "example at ./spec/controllers/my_controller_spec.rb:xxx"
      puts
self.class.example.full_description #=> "MyController PUT update"
      puts example
.description                 #=> undefined local variable or method 'example'

   
end


    it
"should update things" do
      puts
self.class                          #=> RSpec::ExampleGroups::MyController::PUTUpdate
      puts
self.class.example.description      #=> "example at ./spec/controllers/my_controller_spec.rb:xxx"
      puts
self.class.example.full_description #=> "MyController PUT update"
      puts example
.description                 #=> undefined local variable or method 'example'
   
end
 
end
end


I'm seeing some odd results when getting the metadata and description values for examples. 

None of the Example instance methods work. Everything behaves as an ExampleGroup. I'm not sure if this is intended.



it
"does something" do
  expect
(example.metadata[:description]).to eq("does something")
end

This does not work. Am I doing something wrong?

Myron Marston

unread,
Feb 28, 2015, 2:57:30 PM2/28/15
to rs...@googlegroups.com
Our README is out of date -- it was written against RSpec 2 but we have no tooling that validates its correctness and we forgot to update that example for the changes in RSpec 3.  That's one advantage of the cukes -- they are self-validating and thus always correct :).

Anyhow, in RSpec 3, we removed the `example` method, in favor of passing an arg to the `it`, `before`, `after`, `let`, etc. blocks:


So change `before(:example) do` to `before(:example) do |example|` and `it "..." do` to `it "..." do |example|` and it should work.

Everything behaves as an ExampleGroup. I'm not sure if this is intended.

It is.  Every example is evaluated in the context of an instance of the example group class of which it is a part.  This mirrors typical xUnit structure where you've got TestCase classes and tests are instance methods of those classes.  This in turn provides us with the desired scoping and inheritance semantics that you'd want.  Helper methods defined in the example group can be automatically called from within the `it` block without doing anything special to support that.

The `example` object yielded to these methods is a special object that keeps track of the state of a particular example, exposing its metadata, its execution result, etc.

HTH,
Myron

Myron Marston

unread,
Feb 28, 2015, 3:00:07 PM2/28/15
to rs...@googlegroups.com
One other thing that's probably confusing there: when you call `self.class.example` you are defining a new example.  `it` is simply an alias of `example`.  That's why `self.class.example.description` has the location rather than the description of the `it` example -- because you're getting the description off of a new, anonymous example, so it is using the location description as the example description.

Myron 

br...@targetedvictory.com

unread,
Mar 3, 2015, 2:04:16 PM3/3/15
to rs...@googlegroups.com
Got it. Works as expected. Thanks! 

Myron Marston

unread,
Mar 3, 2015, 2:27:50 PM3/3/15
to rs...@googlegroups.com
Want to open a PR fixing our README :)?

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/5fd08772-9a0d-4855-870f-d0aa9e66fd46%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Besnik Ruka

unread,
Mar 3, 2015, 2:36:29 PM3/3/15
to rs...@googlegroups.com
I'll leave it for someone more familiar with functionality/best practices.

--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/JhOMtubpZNY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.

To post to this group, send email to rs...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages