Skip to content

Commit

Permalink
Pickle::Session::ModelNotKnownError is raised instead of a generic Ru…
Browse files Browse the repository at this point in the history
…ntimeError
  • Loading branch information
ianwhite committed Aug 18, 2010
1 parent 2527060 commit 33044f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 6 additions & 0 deletions History.txt
@@ -1,3 +1,9 @@
== master

* 1 minor improvement
* Pickle::Session::ModelNotKnownError is raised instead of a generic RuntimeError


== 0.3.5

* 3 improvements
Expand Down
23 changes: 18 additions & 5 deletions lib/pickle/session.rb
@@ -1,5 +1,18 @@
module Pickle
module Session
class ModelNotKnownError < RuntimeError
attr_reader :name

def initialize(name, message = nil)
@name = name
@message = message || "The model: '#{name}' is not known in this scenario. Use #create_model to create, or #find_model to find, and store a reference in this scenario."
end

def to_s
@message
end
end

class << self
def included(world_class)
proxy_to_pickle_parser(world_class)
Expand Down Expand Up @@ -53,8 +66,8 @@ def find_model(a_model_name, fields = nil)
record
end

def find_model!(a_model_name, fields = nil)
find_model(a_model_name, fields) or raise "Can't find pickle model: '#{a_model_name}' in this scenario"
def find_model!(name, fields = nil)
find_model(name, fields) or raise ModelNotKnownError, name
end

def find_models(factory, fields = nil)
Expand Down Expand Up @@ -87,7 +100,7 @@ def created_model(name)
elsif name_or_index.is_a?(Integer)
models_by_index(factory)[name_or_index]
else
models_by_name(factory)[name_or_index] or raise "Can't find pickle model: '#{name}' in this scenario"
models_by_name(factory)[name_or_index] or raise ModelNotKnownError, name
end
end

Expand All @@ -110,12 +123,12 @@ def model?(name)

# like model, but raise an error if it can't be found
def model!(name)
model(name) or raise "Can't find pickle model: '#{name}' in this scenario"
model(name) or raise ModelNotKnownError, name
end

# like created_model, but raise an error if it can't be found
def created_model!(name)
created_model(name) or raise "Can't find pickle model: '#{name}' in this scenario"
created_model(name) or raise ModelNotKnownError, name
end

# return all original models of specified type
Expand Down
6 changes: 3 additions & 3 deletions spec/pickle/session_spec.rb
Expand Up @@ -257,7 +257,7 @@ class Model

it "should call raise error if find_model returns nil" do
should_receive(:find_model).with('name', 'fields').and_return(nil)
lambda { find_model!('name', 'fields') }.should raise_error(RuntimeError, "Can't find pickle model: 'name' in this scenario")
lambda { find_model!('name', 'fields') }.should raise_error(Pickle::Session::ModelNotKnownError)
end
end

Expand Down Expand Up @@ -425,10 +425,10 @@ def do_create_users
end

it "#model!('unknown') should raise informative error message" do
lambda { model!('unknown') }.should raise_error("Can't find pickle model: 'unknown' in this scenario")
lambda { model!('unknown') }.should raise_error(Pickle::Session::ModelNotKnownError, "The model: 'unknown' is not known in this scenario. Use #create_model to create, or #find_model to find, and store a reference in this scenario.")
end

it "#created_model!('unknown') should raise informative error message" do
lambda { created_model!('unknown') }.should raise_error("Can't find pickle model: 'unknown' in this scenario")
lambda { created_model!('unknown') }.should raise_error(Pickle::Session::ModelNotKnownError)
end
end

0 comments on commit 33044f4

Please sign in to comment.