[rspec-users] What should I test/specify?

Andrew Wagner wagner.andrew at gmail.com
Sat Oct 30 13:35:10 EDT 2010


On Sat, Oct 30, 2010 at 12:46 PM, David Chelimsky <dchelimsky at gmail.com>wrote:

> On Oct 30, 2010, at 11:40 AM, Andrew Wagner wrote:
>
> > Ok, so here's the scenario. I have a top-level module, with a method
> called load_file. It should take a file name, get the YAML module (Syck) to
> parse it, and then send the result to a class, which should convert that
> result into a series of objects.
> >
> > I'm not sure what specs, if any, I should write for load_file. I can get
> into the nitty gritty details of exactly what methods it should be calling,
> but that seems brittle. I can skip all that and just make assertions about
> the final series of objects, but I'd rather put those specs on the class
> that's actually doing the conversion.
> >
> > Suggestions? Here's the code that I (think I) want to drive out:
> >
> > module BTree
> >   def self.load_file file_name
> >     raw = YAML::load_file file_name
> >     tree = BehaviorTreeCreator.new(raw).create
> >   end
> > end
>
> One approach would be:
>
> describe BTree do
>  describe "::load_file" do
>     let(:filename) { '/tmp/btree_example.yml' }
>    let(:content) { "---\na: 1\n" }
>    before { File.open(filename,'w') {|f| f.write content } }
>    after { File.delete filename }
>
>    it "reads and parses the content of the file" do
>      BTree.load_file(filename).should
> eq(BehaviorTreeCreator.new(content).create
>    end
>  end
> end
>
> Assuming there are specs for BehaviorTreeCreator.new(content).create, then
> this tells the story pretty well without being terribly invasive.
>
> WDYT?
>
> David
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>

I like this, though it's missing a YAML::load(content). I.e.,

BTree.load_file(filename).should
eq(BehaviorTreeCreator.new(YAML::load content).create
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20101030/932176a5/attachment.html>


More information about the rspec-users mailing list