rspec-core icon indicating copy to clipboard operation
rspec-core copied to clipboard

Display the names of let() values instead of evaluated result in output.

Open thomasfedb opened this issue 13 years ago • 14 comments

It would be a massive improvement in spec output readability if the names of values instead of the values themselves are provided when using let in a test.

For example:

let(:matched_result) { Object.new }
subject { ... }

it { should include matched_result }

Currently would output something like:

should include #<Object:0x000000015c3cc0>

But would be much more readable if the output was:

should include matched_result

thomasfedb avatar Jan 30 '13 02:01 thomasfedb

I like this idea - not sure how easy it would be to do, though. Would definitely make the output a lot easier to read.

sevenseacat avatar Jan 30 '13 02:01 sevenseacat

As far as implementation goes, I think it could be done with using a proxy object to wrap the results of let blocks.

thomasfedb avatar Jan 30 '13 02:01 thomasfedb

+1

dangalipo avatar Jan 30 '13 02:01 dangalipo

+1

could also be done by modifying the metaclass of the object resulting from the let. I would have concerns about backwards compatibility. Perhaps rather than modifying inspect it would be better to define a new method and update matchers to use that, if it were present.

def let(name, &block)
  define_method(name) do
    __memoized.fetch(name) do |k|
      obj = instance_eval(&block)
      metaclass = class << obj; self; end
      metaclass.__send__(:define_method, :inspect) { name.to_s.gsub('_', ' ') }
      __memoized[k] = obj
    end
  end
end

delautour avatar Jan 30 '13 03:01 delautour

I like this idea quite a bit, but I'm currently leaning against inclusion in rspec-core for a few reasons:

  • Tests often give me useful feedback about what to_s or inspect is returning from my objects (driving me to define more useful, human-readable versions of those methods), due to expectation failure messages. This would silence that kind of feedback.
  • As useful as this would be in some situations, I think there are other situations where I would not want this: consider that the let name of the object is almost certainly going to contain less information than what to_s or inspect provide (especially if those methods have been customized to highlight important info).
  • Given that, I think this would need to be an opt-in feature, but if we're going to make it opt-in, my preference is for it to be in an external gem (that users can opt-in to by installing and loading) than to complicate the plethora of config options already in rspec.
  • All of the ways I've thought of implementing this have serious downsides that I would consider unacceptable for inclusion in rspec-core. We could do a proxy object as @thomasfedb suggest, but that can lead to serious confusion. Proxy objects have their uses, but their are certain ruby semantics (e.g. falsiness) that you can't get from a proxy object, plus it creates confusion when the object_id isn't what you expect. I think a proxy object would be too "magical" and lead to confusion. @Amerdrix's idea of defining a singleton method has merit, but as I understand it from at least one source doing so blows away method caches and imposes significant runtime performance penalties.
  • There's also the potential confusion caused by spec'ing an object's inspect method, and finding the spec suddenly start failing because it is created in a let block.

Does someone want to take a stab at creating an external gem with this feature? If you find it is difficult to do so w/o monkey patching rspec-core, I'd be happy to improve rspec's APIs here so that you can cleanly add this feature.

myronmarston avatar Jan 30 '13 05:01 myronmarston

There are additional issues with the approach I suggested. Making it infeasible in so far as I can see.

  • Values (symbols and numbers at least) can't have a singleton.
  • You cannot use Marshal.dump on an objects whose singleton has been modified in this way.
mc = class << :foo; self; end
=> TypeError: can't define singleton

obj = {}
def obj.inspect; true; end
Marshal.dump(obj)
=> TypeError: singleton can't be dumped

delautour avatar Jan 30 '13 06:01 delautour

The problems mentioned by @Amerdix and @myronmarston are, oddly enough, for the most part already solved in libraries such as rspec-mock and rr. In fact, interestingly, objects mocked by rspec-mock work fine with Marshal.dump, however those mocked with rr don't, because rspec-mock includes a fix that deals with this specifically.

The remaining problems then are:

  • Making this feature mocking framework agnostic. (And potentially patching other frameworks to work with Marshal.dump.)
  • Can we hijack inspect?
  • If not, how best to patch/monkeypatch existing matchers.

thomasfedb avatar Feb 02 '13 08:02 thomasfedb

@thomasfedb, I'm not so sure. In fact I have already have a comment about this is one of my projects:

#rspec_mocks turns ContactFormController into an anonymous class which Marshal.dump can't handle.

delautour avatar Feb 02 '13 08:02 delautour

@Amerdrix interesting, perhaps it only works on instances?

thomasfedb avatar Feb 02 '13 08:02 thomasfedb

I'm working on this here: https://github.com/thomasfedb/rspec-core/tree/let-block-descriptions

I think I've worked out a solution that would be able to exist within rpec/rspec-core without breaking compatibility.

# standard let block
let(:blarg) { "i am a let" } 

# named let block
let(:blarg).named { "i am a named block" }

# named let block using a proxy object
let(:blarg).named.proxy { "i am a named block using a proxy" }

# named let block using a singleton method
let(:blarg).named.singleton { "i am a named block using a ingleton method" }

Credit to @Amerdrix for the concept.

I have implimented the above in this commit: https://github.com/thomasfedb/rspec-core/commit/60594341e2056e887c318cbb736796ef74e14c6c

Thoughts? @myronmarston

thomasfedb avatar Feb 04 '13 14:02 thomasfedb

Thoughts? @myronmarston

In no particular order...

  • I think the let(:blarg).named syntax is kinda neat for opting in to to this feature.
  • That said, I think it reads funny; with a method like named, I'd expect an argument for what it should be named to come after. Maybe a named_let(:blarg) would be better?
  • Having the .proxy and .singleton methods is nifty and nicely flexible...but I imagine that the average user will not know enough to know which they should pick. Presenting them with the option may lead to confusion.
  • I'm still concerned about the singleton approach busting method caches and creating perf degredations. Is it really needed? Might be better just to use the proxy approach, and users can either use it or nothing.
  • It's not clear to me how rspec_description will wind up in failure messages. Will we have to update all matchers to use it? What about custom matchers defined by end users?

Overall, I still think this fits better in an external gem, rather than in being in rspec-core itself. We may need to restructure things in rspec-core to provide a clean public API for such a gem to hook into so that it can provide this functionality w/o using brittle monkey patching, however, and I'd be totally in favor of such a change, regardless.

@alindeman / @dchelimsky -- what are your thoughts on whether or not this belongs in rspec-core vs an external gem?

myronmarston avatar Feb 05 '13 01:02 myronmarston

Looks like there's a gem that already does this:

http://yuroyoro.github.com/named_let/

Does that meet your needs?

myronmarston avatar Mar 21 '13 14:03 myronmarston

I'd say that gem meets the original topic of this discussion, can we publicise that in some way to help our users discover it?

JonRowe avatar Mar 25 '13 23:03 JonRowe

I'd say that gem meets the original topic of this discussion, can we publicise that in some way to help our users discover it?

Good question. We don't yet have an "RSpec extension gem repository" but it would be quite useful for end users, I think.

myronmarston avatar Mar 25 '13 23:03 myronmarston