copy-edit viget posts

This commit is contained in:
David Eisinger
2023-10-24 20:48:09 -04:00
parent 0438a6d828
commit f86f391e82
77 changed files with 1663 additions and 1380 deletions

View File

@@ -2,7 +2,6 @@
title: "Shoulda Macros with Blocks"
date: 2009-04-29T00:00:00+00:00
draft: false
needs_review: true
canonical_url: https://www.viget.com/articles/shoulda-macros-with-blocks/
---
@@ -20,14 +19,40 @@ Fortunately, since we're using
[Shoulda](http://thoughtbot.com/projects/shoulda/), we were able to DRY
things up considerably with a macro:
``` {#code .ruby}
class Test::Unit::TestCase def self.should_sum_total_ratings klass = model_class context "finding total ratings" do setup do @ratable = Factory(klass.to_s.downcase) end should "have zero total ratings if no rated talks" do assert_equal 0, @ratable.total_ratings end should "have one total rating if one delivery & content rating" do talk = block_given? ? yield(@ratable) : @ratable Factory(:content_rating, :talk => talk) Factory(:delivery_rating, :talk => talk) assert_equal 1, @ratable.reload.total_ratings end end end end
```ruby
class Test::Unit::TestCase
def self.should_sum_total_ratings
klass = model_class
context "finding total ratings" do
setup do
@ratable = Factory(klass.to_s.downcase)
end
should "have zero total ratings if no rated talks" do
assert_equal 0, @ratable.total_ratings
end
should "have one total rating if one delivery & content rating" do
talk = block_given? ? yield(@ratable) : @ratable
Factory(:content_rating, :talk => talk)
Factory(:delivery_rating, :talk => talk)
assert_equal 1, @ratable.reload.total_ratings
end
end
end
end
```
This way, if we're testing a talk, we can just say:
``` {#code .ruby}
class TalkTest < Test::Unit::TestCase context "A Talk" do should_sum_total_ratings end end
```ruby
class TalkTest < Test::Unit::TestCase
context "A Talk" do
should_sum_total_ratings
end
end
```
But if we're testing something that has a relationship with multiple
@@ -35,12 +60,16 @@ talks, our macro accepts a block that serves as a factory to create a
talk with the appropriate relationship. For events, we can do something
like:
``` {#code .ruby}
class EventTest < Test::Unit::TestCase context "An Event" do should_sum_total_ratings do |event| Factory(:talk, :event => event) end end end
```ruby
class EventTest < Test::Unit::TestCase
context "An Event" do
should_sum_total_ratings do |event|
Factory(:talk, :event => event)
end
end
end
```
I\'m pretty happy with this solution, but having to type "event" three
times still seems a little verbose. If you\'ve got any suggestions for
I'm pretty happy with this solution, but having to type "event" three
times still seems a little verbose. If you've got any suggestions for
refactoring, let us know in the comments.