Skip to content

Instantly share code, notes, and snippets.

@danielsz
Created August 11, 2012 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsz/3321662 to your computer and use it in GitHub Desktop.
Save danielsz/3321662 to your computer and use it in GitHub Desktop.
def is my stub framework
## original code
describe "#pubdate" do
describe "before publishing" do
it "is blank" do
@it.pubdate.must_be_nil
end
end
describe "after publishing" do
before do
@clock = stub!
@now = DateTime.parse("2011-09-11T02:56")
stub(@clock).now(){@now}
@it.blog = stub!
@it.publish(@clock)
end
it "is a datetime" do
@it.pubdate.class.must_equal(DateTime)
 end
it "is the current time" do
@it.pubdate.must_equal(@now)
end
end
end
## suggested code
describe "#pubdate" do
describe "before publishing" do
it "is blank" do
@it.pubdate.must_be_nil
end
end
describe "after publishing" do
before do
class Clock < DateTime
def now
DateTime.parse("2011-09-11T02:56")
end
end
@clock = Clock.new
@blog = MiniTest::Mock.new
@blog.expect :add_entry, nil, [@it]
@it.blog = @blog
@it.publish(@clock)
end
after do
@blog.verify
end
it "is a datetime" do
@it.pubdate.class.must_equal(DateTime)
end
it "is the current time" do
@it.pubdate.must_equal(@clock.now)
end
end
end
## or better yet
describe "after publishing" do
before do
class Clock < DateTime
def now
self.class.parse("2011-09-11T02:56")
end
end
...
end
...
it "is a datetime" do
@it.pubdate.class.must_equal(Clock)
end
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment