features/safe_stubbing/spies.feature in bogus-0.1.0 vs features/safe_stubbing/spies.feature in bogus-0.1.1
- old
+ new
@@ -3,18 +3,21 @@
Object Oriented Programming is all about messages sent between the objects. If you follow principles like "Tell, Don't Ask", you will enable yourself to combine Bogus's powerful feature of faking objects with it's ability to verify object interactions.
Typically, stubbing libraries force you to first stub a method, so that you can later make sure it was called. However, if you use fakes, Bogus lets you verify that a method was called (or not) without stubbing it first.
Background:
- Given a file named "foo.rb" with:
+ Given a file named "library.rb" with:
"""ruby
class Library
def checkout(book)
# marks book as checked out
end
end
+ """
+ Given a file named "student.rb" with:
+ """ruby
class Student
def initialize(library)
@library = library
end
@@ -27,10 +30,13 @@
"""
Scenario: Ensuring methods were called
Then spec file with following content should pass:
"""ruby
+ require_relative 'student'
+ require_relative 'library'
+
describe Student do
fake(:library)
it "studies using books from library" do
student = Student.new(library)
@@ -44,10 +50,13 @@
"""
Scenario: Spying on methods that do not exist
Then spec file with following content should fail:
"""ruby
+ require_relative 'student'
+ require_relative 'library'
+
describe Student do
fake(:library)
it "studies using books from library" do
student = Student.new(library)
@@ -60,10 +69,13 @@
"""
Scenario: Spying on methods with wrong number of arguments
Then spec file with following content should fail:
"""ruby
+ require_relative 'student'
+ require_relative 'library'
+
describe Student do
fake(:library)
it "studies using books from library" do
student = Student.new(library)
@@ -77,17 +89,56 @@
"""
Scenario: Spying on previously stubbed methods
Then spec file with following content should pass:
"""ruby
+ require_relative 'student'
+ require_relative 'library'
+
describe Student do
fake(:library)
it "studies using books from library" do
stub(library).checkout("Moby Dick") { "checked out" }
library.checkout("Moby Dick").should == "checked out"
library.should have_received.checkout("Moby Dick")
+ end
+ end
+ """
+
+ Scenario: Spying on attribute writers
+ Given a file named "canvas.rb" with:
+ """ruby
+ class Canvas
+ def background_color=(color)
+ # do something complicated
+ end
+ end
+ """
+
+ Given a file named "popup.rb" with:
+ """ruby
+ class Popup
+ def self.alert(message, canvas)
+ canvas.background_color = "red"
+ # display message
+ end
+ end
+ """
+
+ Then spec file with following content should pass:
+ """ruby
+ require_relative 'canvas'
+ require_relative 'popup'
+
+ describe Popup do
+ fake(:canvas)
+
+ it "sets the background to red" do
+ Popup.alert("No such file!", canvas)
+
+ canvas.should have_received(:background_color=, "red")
end
end
"""