The symbol "subject" by default is an instance of the described class. In some method testing I often want to set the subject to be a method call to the instance, for example "city.parse_input()". I tried to do describe ''#parse_input'' do subject{ subject.parse_input() } #returns hash ... end but I can''t. I want to ask where did I get the semantic of this subject command wrong which prevent me from doing this. Thanks.
Andrew Timberlake
2012-Jan-05 16:31 UTC
[rspec-users] Can I set subject to its own method call?
Why don''t you use let: describe MyClass do # Will set subject to an instance of MyClass context ''#parse_input'' do let(:input) { subject.parse_input } # calling input in your specs will now return the result of parse_input it ''is a hash'' do input.should be_a(Hash) # This evaluates let(:input) end end end Hope that helps. Andrew On Thursday 05 January 2012 at 11:26 AM, m wrote:> The symbol "subject" by default is an instance of the described class. > In some method testing I often want to set the subject to be a method > call to the instance, for example "city.parse_input()". > > I tried to do > > describe ''#parse_input'' do > subject{ subject.parse_input() } #returns hash > ... > end > > but I can''t. > > I want to ask where did I get the semantic of this subject command > wrong which prevent me from doing this. Thanks. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org (mailto:rspec-users at rubyforge.org) > http://rubyforge.org/mailman/listinfo/rspec-users > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120105/302a17ba/attachment.html>
Thanks, I was using another way before until I properly read through the rspec doc. I was doing this in another example: let(:instance) { described_class.new() } describe '' subject { instance.parse_log() } its([:duration]){ should == mock_duration} its([:owner]){ should == mock_editor} I guess it just feels more natural to me to put the describe method call in the center of the spec, which is the subject. And it is more abbreviated (I don''t have to put a shoulda in a it block which is three lines). On 1?6?, ??12?31?, Andrew Timberlake <and... at andrewtimberlake.com> wrote:> Why don''t you use let: > > describe MyClass do ? ? ? ? ? ? ? ? ? ? ? ? # Will set subject to an instance of MyClass > ? context ''#parse_input'' do > ? ? let(:input) { subject.parse_input } ? ? # calling input in your specs will now return the result of parse_input > > ? ? it ''is a hash'' do > ? ? ? input.should be_a(Hash) ? ? ? ? ? ? ?# This evaluates let(:input) > ? ? end > ? end > end > > Hope that helps. > > Andrew > > On Thursday 05 January 2012 at 11:26 AM, m wrote: > > > > > > > > > The symbol "subject" by default is an instance of the described class. > > In some method testing I often want to set the subject to be a method > > call to the instance, for example "city.parse_input()". > > > I tried to do > > > describe ''#parse_input'' do > > subject{ subject.parse_input() } #returns hash > > ... > > end > > > but I can''t. > > > I want to ask where did I get the semantic of this subject command > > wrong which prevent me from doing this. Thanks. > > _______________________________________________ > > rspec-users mailing list > > rspec-us... at rubyforge.org (mailto:rspec-us... at rubyforge.org) > >http://rubyforge.org/mailman/listinfo/rspec-users > > > > _______________________________________________ > rspec-users mailing list > rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users