Permalink
Browse files

add spec for file cache

  • Loading branch information...
1 parent be79998 commit 804932fe53906b35ef6683cb02b98cee7769711f @jimweirich committed Sep 14, 2012
Showing with 35 additions and 1 deletion.
  1. +7 −1 lib/rspec/given/file_cache.rb
  2. +28 −0 spec/lib/rspec/given/file_cache_spec.rb
@@ -6,7 +6,13 @@ def initialize
end
def get(file_name)
- @lines[file_name] ||= open(file_name) { |f| f.readlines }
+ @lines[file_name] ||= read_lines(file_name)
+ end
+
+ private
+
+ def read_lines(file_name)
+ open(file_name) { |f| f.readlines }
end
end
end
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+module RSpec
+ module Given
+
+ DESCRIBE_LINE = __LINE__
+ describe FileCache do
+ Given(:file_name) { "spec/lib/rspec/given/file_cache_spec.rb" }
+ Given(:cache) { FileCache.new }
+
+ When(:result) { cache.get(file_name) }
+
+ context "when reading the file" do
+ Then { result[DESCRIBE_LINE].should =~ /describe FileCache do/ }
+ Then { result.size.should == MAX_LINE }
+ end
+
+ context "when getting the same file twice" do
+ Given { cache.should_receive(:read_lines).once.and_return(["A"]) }
+ When(:result2) { cache.get(file_name) }
+ Then { result.should == ["A"] }
+ Then { result2.should == ["A"] }
+ end
+ end
+ end
+end
+
+MAX_LINE = __LINE__

0 comments on commit 804932f

Please sign in to comment.