Skip to content

Specs for aliased methods #1364

@Earlopain

Description

@Earlopain

Ruby has many methods that refer to exactly the same thing. For example Hash#include?/Hash#key?/Hash#has_key?/Hash#member?.

ruby-spec handles this via shared examples that then get run once for each method. This inflates the passing/failing spec rate, since there must never be different behavior between them. If one spec passes, it will pass for the others and same if it fails. It also gives indirection when reading the specs, mostly via passing the method name as a parameter to the shared examples.

My question is if this is good usage of shared examples. Wouldn't a better test look something like this?

it "it an alias of Hash#key" do
  # Checks that they are actually identical
  Hash.instance_method(:include?).should == Hash.instance_method(:key?)
end

One method would then inline the shared examples to actually run the tests. Which one tests for being an alias and which one runs the tests doesn't really matter, but to reduce debate it can be the one with the "real" implementation in MRI ("also aliased as" when viewing docs).

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions