Skip to content

Commit

Permalink
Changes file requiring - template and stub helpers need to be properly
Browse files Browse the repository at this point in the history
named.
  • Loading branch information
LFDM committed Oct 4, 2013
1 parent f5cf6cf commit d0aab72
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/stub_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def require_path(rel_path)
path = File.expand_path(rel_path)

if File.exists?(path)
require "#{path}" if File.file?(path)
require "#{path}" if File.file?(path) && path.match(/\/(stub_|template_)[^\/]*$/)
Dir["#{path}/*"].each { |file| require_path(file) }
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/factories/wrong_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
StubFactory.define_template(:wrong_template) do
{ test: 11 }
end
17 changes: 14 additions & 3 deletions spec/lib/stub_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ class A; attr_reader :test; end
expect { StubFactory.define_helper(:helper2, :A) }.to raise_error(StubFactory::HelperError)
end

it "helpers can be defined in files - default path is spec/support/helpers" do
# required_helper is defined in #spec/support/helper_test.rb
it "helpers can be defined in files called stub_***.rb - default path is spec/support/helpers" do
# required_helper is defined in #spec/support/stub_helper_test.rb
stub_required_helper.should be_an_instance_of A
end

it "to allow other files in this folder wrongly named files are not consumed" do
# wrong_helper is defined in #spec/support/wrong_helper_test.rb
expect { stub_wrong_helper }.to raise_error NameError
end
end

describe "#new_stub" do
Expand Down Expand Up @@ -78,10 +83,16 @@ class A; attr_reader :test; end
o.test.should be_nil
end

it "templates can be defined in files - their default path is spec/factories" do
it "templates can be defined in files called template_**.rb - their default path is spec/factories" do
# required_template is defined in #spec/factories/template_test.rb
A.new_stub(template: :required_template).test.should == 11
end

it "to allow other files in this folder wrongly named files are not consumed" do
# wrong_template is defined in #spec/factories/wrong_test.rb
# with the variable test == 11
A.new_stub(template: :wrong_template).test.should be_nil
end
end

context "when a custom template exists" do
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions spec/support/helpers/wrong_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
StubFactory.define_helper(:wrong_helper, :A)

0 comments on commit d0aab72

Please sign in to comment.