Permalink
Browse files

[NEW] First commit. (Finally getting this into a repo.)

  • Loading branch information...
0 parents commit 38c94e85647032a3752c0e84a5ff73e2c2d9780a Andrew Kasper committed Jan 26, 2012
Showing with 117 additions and 0 deletions.
  1. +49 −0 README
  2. +20 −0 lib/assignment_helper.rb
  3. +23 −0 lib/interface_helper.rb
  4. +15 −0 lib/persistence_helper.rb
  5. +10 −0 lib/rface.rb
49 README
@@ -0,0 +1,49 @@
+You need a new class. Some dev on your team is *insisting* that everyone use
+BDD and RSpec. You're not opposed, except that it takes so much time to
+figure out where to even begin! "Write a failing spec first?" Easier said
+than done.
+
+What you really want is to write code. Plain and simple. RSpec should
+facilitate that, not get in the way of it. But here you are, trying to
+figure out the simplest possible thing you could possibly make this spec
+fail in a way that would actually look like progress.
+
+Enter: RFace.
+
+
+How to use it:
+
+# "Well, I know I'm going to need a method named..."
+describe InterfaceExample do
+ include InterfaceHelper
+
+ subject {described_class.new}
+
+ implements :some_instance_method, :some_other_instance_method
+ # InterfaceExample defines #some_instance_method, #some_other_instance_method
+
+ class_implements :some_class_method, :some_other_class_method
+ # InterfaceExample defines .some_class_method, .some_other_class_method
+end
+
+# "I'm probably going to have a simple attribute named..."
+describe AssignmentExample do
+ include AssignmentHelper
+
+ subject {described_class.new}
+
+ sets :foo => 1, :bar => 2
+ # Verifies that it is possible to assign subject.foo=1 or subject.bar=2 and
+ # that value will not change without subsequent assignment.
+end
+
+# "There will probably be an attribute that lives in the DB named..."
+describe PersistenceExample do
+ include PersistenceHelper
+
+ subject {described_class.new}
+
+ persists :foo => 1, :bar => 2
+ # Verifies that subject can be #save'd, #reload'ed, and that #foo will be 1
+ # between save and reload, #bar will be 2 between save and reload.
+end
@@ -0,0 +1,20 @@
+module PersistenceHelper
+ #Verifies that the subject receives :<field>= for each field when calling subject.<using>(<with>)
+ def sets(*args)
+ params = args.extract_options!
+ m = params[:method]
+ p = Array.wrap(params[:params])
+
+ args.each do |field|
+ it "sets the #{field}" do
+ subject.should_receive(:"#{field}=")
+ subject.send(:"#{m}", *p)
+ end
+
+ it "assigns the #{field}" do
+ subject.send(:"#{m}", *p)
+ subject.send(:"#{field}").should_not be_nil
+ end
+ end
+ end
+end
@@ -0,0 +1,23 @@
+#Offers helper methods for all model classes
+
+module InterfaceHelper
+ def self.inherited(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ #Given that #subject has been called within the spec, takes an array of symbols (method names)
+ def implements(*methods)
+ methods.each do |method|
+ it("implements ##{method}") {subject.should respond_to(method)}
+ end
+ end
+
+ def class_implements(*methods)
+ methods.each do |method|
+ it("implements .#{method}") {described_class.should respond_to(method)}
+ end
+ end
+ end
+end
+
@@ -0,0 +1,15 @@
+module PersistenceHelper
+ #Given that .subject has been set within the spec, takes a hash of fields mapping to valid
+ # assignment values. These values should differ from the value at the time the model is passed in.
+ def persists(fields={})
+ it("saves") { subject.should respond_to(:save) }
+ it("reloads") { subject.should respond_to(:reload) }
+ fields.each_pair do |field, valid_value|
+ it("persists #{field}") do
+ subject.send(field.to_s + '=', valid_value)
+ subject.save
+ subject.reload.send(field).should == valid_value
+ end
+ end
+ end
+end
@@ -0,0 +1,10 @@
+module RFace
+ def self.inherited(base)
+ base.instance_eval do
+ include InterfaceHelper
+ include AssignmentHelper
+ include PersistenceHelper
+ end
+ end
+end
+

0 comments on commit 38c94e8

Please sign in to comment.