On Saturday, February 28, 2015 at 11:57:30 AM UTC-8, Myron Marston wrote:
>
> On Saturday, February 28, 2015 at 11:48:01 AM UTC-8, 
> [email protected] wrote:
>>
>> 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.
>>
>> From the rspec-core README https://github.com/rspec/rspec-core#metadata
>>
>>
>> it "does something" do 
>>   expect(example.metadata[:description]).to eq("does something") 
>> end
>>
>> This does not work. Am I doing something wrong?
>>
>
> 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:
>
>
> http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#dsl-methods-yield-the-example-as-an-argument
>
> 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
>

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 

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/c83d15a1-87ae-407c-ab1a-d53b2fa72052%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to