[enhancement] advanced subject

13 views
Skip to first unread message

Kelly Stannard

unread,
Aug 1, 2014, 8:46:02 PM8/1/14
to rs...@googlegroups.com
So, as we all know, we shouldn't use subject. But what if you really like one liner syntax?

I am working on an attempt to make subject more dynamic and allow the use of one lines in more places. I am doing this by parsing describe arguments and adding a couple new blocks in order to build the subject dynamically.

For example:

describe Object do
  describe '#nil?' do
    it { should be_false }
  end
end

Will be parsed to know that the subject under test is the following: Object.new.nil?

What about class methods and methods with arguments?

describe File do
  describe '.read' do
    when_passed 'file.txt' do
      it { should eq('contents of file.txt') }
    end
  end
end

This will be parsed as
File.read('file.txt'). This works by storing the method, what the method is called on, and what is passed to the method and putting it all together within a proc that is called as the hidden subject. A period before the method indicates it is a class method and a hash indicates it is an instance method.

Finally there is a way to pass variables to the initializer:


describe Array do
  when_initialized_with 2 do
    it { should eq [nil, nil] }
  end
end

This is just the basics and I am looking for feedback and more feature ideas. Currently I am trying to figure out passing blocks and how #let fits in.

rspec-advanced_subject



Myron Marston

unread,
Aug 1, 2014, 9:24:28 PM8/1/14
to rs...@googlegroups.com
On Friday, August 1, 2014 5:46:02 PM UTC-7, Kelly Stannard wrote:
So, as we all know, we shouldn't use subject.

I wouldn't say that.  I would say that as David explained in the article, `subject` is rarely the best name for a particular object in a test, and using a more-intention revealing test will generally make  your tests more clear and readable.  But `subject` exists and has a completely valid purpose.  Using it is fine, but please understand how it works and what tradeoffs you are making.
 
But what if you really like one liner syntax?

That's fine; the one liner syntax is the primary reason `subject` even exists.  I personally use the one liner syntax very, very rarely, but I've also seen it used well.  I think it works well with shoulda's matchers, for example.  Or in mustermann:


It works quite well for cases like that.

So if you like the one-liner syntax, understand how it works, and get benefit from it, then use it!

This is just the basics and I am looking for feedback and more feature ideas. Currently I am trying to figure out passing blocks and how #let fits in.

rspec-advanced_subject



Your gem looks interesting; thanks for sharing :).

Myron 

Kelly Stannard

unread,
Aug 2, 2014, 12:14:32 PM8/2/14
to rs...@googlegroups.com
Thanks Myron. I am very glad you think it is interesting.

I was being a bit tongue in check when I was saying not to use subject. I personally use it constantly at work, which it what lead me to make this gem.

I should have also mentioned that the original reason I started writing this gem was because I was tired of stating what method I was testing in the description and then having to re-iterate that when defining the subject. Especially early in a class's life I would run into problems where I would change a method name and forget to change the description and other duplication problems. This dries up that problem.

Kelly
Reply all
Reply to author
Forward
0 new messages