Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ModelDefinition::StubProxy so that you can relate stubs to othe…
…r stubs that haven't been created yet.
- Loading branch information
1 parent
90e93a5
commit 2d72b3d
Showing
9 changed files
with
215 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module ModelStubbing | ||
# Used when attaching stubs to other stubs in definitions. The stub doesn't | ||
# actually have to exist until the Definition is being inserted into the database. | ||
# | ||
# model Post do | ||
# stub :title => 'foo', :author => users(:default) | ||
# end | ||
# | ||
# You can also call specific methods from the stub. | ||
# | ||
# model Blog do | ||
# stub :latest_post => posts(:default), :latest_post_title => posts(:default).title | ||
# end | ||
# | ||
class StubProxy | ||
attr_reader :method_name, :proxy_definition, :proxy_model_name, :proxy_stub_name | ||
|
||
def initialize(definition, model_name, stub_name, method_name = nil) | ||
@proxy_definition = definition | ||
@proxy_model_name = model_name | ||
@proxy_stub_name = stub_name | ||
@method_name = method_name | ||
@model = @stub = nil | ||
end | ||
|
||
def record | ||
@stub ||= fetch_stub | ||
@method_name ? @stub.record_without_stubs.send(@method_name) : @stub.record_without_stubs | ||
end | ||
|
||
alias_method :record_without_stubs, :record | ||
|
||
def ==(other) | ||
if other.is_a?(StubProxy) | ||
other.proxy_definition == @proxy_definition && other.proxy_model_name == @proxy_model_name && other.proxy_stub_name == @proxy_stub_name && other.method_name == @method_name | ||
elsif !@method_name && other.is_a?(Stub) | ||
other.model.definition == @proxy_definition && other.model.name == @proxy_model_name && @proxy_stub_name == other.name | ||
else | ||
super | ||
end | ||
end | ||
|
||
def id | ||
method_missing(:id) | ||
end | ||
|
||
def inspect | ||
"(ModelStubbing::StubProxy[#{@proxy_model_name}(#{@proxy_stub_name.inspect})#{".#{@method_name}" if @method_name}]" | ||
end | ||
|
||
protected | ||
def fetch_stub | ||
@model = @proxy_definition.models[@proxy_model_name] | ||
if @model.nil? | ||
raise "No #{@proxy_model_name.inspect} model found when calling #{@proxy_model_name}(#{@proxy_stub_name.inspect})" | ||
end | ||
@stub = @model.stubs[@proxy_stub_name] | ||
if @stub.nil? | ||
raise "No #{@proxy_stub_name.inspect} stub found in the #{@proxy_model_name.inspect} model when calling #{@proxy_model_name}(#{@proxy_stub_name})" | ||
else | ||
@stub | ||
end | ||
end | ||
|
||
def method_missing(name, *args) | ||
if args.empty? | ||
@method_name = name.to_sym | ||
self | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require File.join(File.dirname(__FILE__), 'spec_helper') | ||
|
||
module ModelStubbing | ||
describe StubProxy do | ||
before :all do | ||
@definition = ModelStubbing.definitions[:default] | ||
@model = @definition.models[:model_stubbing_tags] | ||
@stub = @model.stubs[:foo] | ||
end | ||
|
||
describe "with model name and stub name" do | ||
before :all do | ||
@stub_proxy = StubProxy.new(@definition, @model.name, @stub.name) | ||
end | ||
|
||
it "has no #method_name" do | ||
@stub_proxy.method_name.should be_nil | ||
end | ||
|
||
it "returns @stub.record for #record" do | ||
@stub_proxy.record.should == @stub.record | ||
end | ||
|
||
it "equals the stub" do | ||
@stub_proxy.should == @stub | ||
end | ||
end | ||
|
||
describe "with model name, stub name, and method name" do | ||
before :all do | ||
@stub_proxy = StubProxy.new(@definition, @model.name, @stub.name) | ||
@stub_proxy.name | ||
end | ||
|
||
it "has #method_name" do | ||
@stub_proxy.method_name.should == :name | ||
end | ||
|
||
it "returns @stub.record.name for #record" do | ||
@stub_proxy.record.should == @stub.record.name | ||
end | ||
|
||
it "does not equal the stub" do | ||
@stub_proxy.should_not == @stub | ||
end | ||
end | ||
|
||
describe "missing valid stub name" do | ||
before :all do | ||
@stub_proxy = StubProxy.new(@definition, @model.name, :abc) | ||
end | ||
|
||
it "raises exception with #record" do | ||
lambda { @stub_proxy.record }.should raise_error | ||
end | ||
end | ||
|
||
describe "missing valid model name" do | ||
before :all do | ||
@stub_proxy = StubProxy.new(@definition, :abc, @stub.name) | ||
end | ||
|
||
it "raises exception with #record" do | ||
lambda { @stub_proxy.record }.should raise_error | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.