Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Created March 20, 2012 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stevenharman/2142518 to your computer and use it in GitHub Desktop.
Save stevenharman/2142518 to your computer and use it in GitHub Desktop.
Using Rspec's "let" inside an "it" block. Crazy? Yes. Useful? Occasionally.
describe "#mentions_story?" do
subject { described_class.new(file) }
let(:file) { "COMMIT_EDITMSG" }
before do
File.stub(:read).with(file) { commit_message_text }
end
context "commit message contains the special Pivotal Tracker story syntax" do
it "matches just the number" do
let(:commit_message_text) { example_commit_message("[#8675309]") }
subject.should be_mentions_story("8675309")
end
it "matches state change and number" do
let(:commit_message_text) { example_commit_message("[Fixes #8675309]") }
subject.should be_mentions_story("8675309")
end
end
context "commit message doesn't contain the special Pivotal Tracker story syntax" do
it "doesn't match without brackets" do
let(:commit_message_text) { example_commit_message("#8675309") }
subject.should_not be_mentions_story("8675309")
end
end
end
@mikelikesbikes
Copy link

Why not just create a method to call instead of the lets? I don't think they add any clarity to the examples. Something like this:

describe "#mentions_story?" do
  subject { described_class.new(file) }
  let(:file) { "COMMIT_EDITMSG" }
  def stub_commit_message(message)
    File.stub(:read).with(file) { message }
  end

  context "commit message contains the special Pivotal Tracker story syntax" do
    it "matches just the number" do
      stub_commit_message(example_commit_message("[#8675309]"))
      subject.should be_mentions_story("8675309")
    end

    it "matches state change and number" do
      stub_commit_message(example_commit_message("[Fixes #8675309]"))
      subject.should be_mentions_story("8675309")
    end
  end

  context "commit message doesn't contain the special Pivotal Tracker story syntax" do
    it "doesn't match without brackets" do
      stub_commit_message(example_commit_message("#8675309"))
      subject.should_not be_mentions_story("8675309")
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment