Skip to content

Add support for subject(:name) { ... } #619

Closed
dchelimsky opened this Issue May 15, 2012 · 13 comments

5 participants

@dchelimsky
RSpec member
describe Article do
  subject(:article) { Article.new }
  it { should support_one_liners }
  it "should be exposed with the name :article" do
    article.should support_intention_revealing_name
  end
end

See the following for background:

http://blog.davidchelimsky.net/2012/05/13/spec-smell-explicit-use-of-subject/
https://gist.github.com/2705101
https://gist.github.com/2705200

@avdi
avdi commented May 15, 2012

Heh, I was just about to suggest exactly that :-)

@soulcutter
RSpec member

Good deal.

@dchelimsky dchelimsky added a commit that closed this issue May 16, 2012
@dchelimsky dchelimsky Support a 'name' arg to subject declaration
Closes #619.

Example:

    describe Article do
      subject(:article) { Article.new }
      it { supports_one_liners }
      it "supports intention revealing name" do
        article.should do_something
      end
    end
2e20683
@nicksieger

Next step would be to deprecate Example#subject :)

@dchelimsky
RSpec member

@nicksieger I could deprecate it in the docs, but not programatically, because it's needed for one-liners.

@avdi
avdi commented May 16, 2012
@nicksieger

Oh, of course. #subject is used internally in the framework.

@dchelimsky dchelimsky added a commit that referenced this issue May 16, 2012
@dchelimsky dchelimsky doc tweaks [ci skip] #619 5ea6d86
@dchelimsky
RSpec member

@avdi I'd argue that in most cases you can do better than "subject", even in shared specs.

@dchelimsky
RSpec member

@avdi ... unless you're using the implicit subject built from the described_class, but then you could just reference described_class, which I think is a more descriptive name than subject. YMMV :)

@justinko

I'd argue that in most cases you can do better than "subject", even in shared specs.

All methods of sharing accept arguments (and soon to be blocks). :metal:

@avdi
avdi commented May 16, 2012
@dchelimsky
RSpec member

@avdi - but that protocol has a name. If the specs are for things that implement that protocol, then you can use that name or something similar. If you're spec'ing a consumer of the protocol, then "consumer" might be a good name. "subject" doesn't tell you anything about either, unless it's one of the subjects who bow to you in your kingdom.

@avdi
avdi commented May 16, 2012
@dchelimsky
RSpec member

@avdi if you were going to add shared specs from my fribble-protocol lib, you'd probably start with docs because you'd need to know the name to pass to it_behaves_like, and those same docs would tell you what to put in the block. But even if you failed to read that part, you'd get a NameError when the shared spec references fribble_drinker, or the shared spec could detect the presence of the method first and then, with a friendly tone, guide you to "awful awful" happiness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.