spec tips(keep adding)

1.stub meet "one to many" relationship

 

Here is the code:

describe "original_content" do
    it "should generate page_group's original_content" do 
      page_group = PageGroup.create
      page1 = page_group.pages.create
      page1.stub!(:content).and_return("page1 content")
      page2 = page_group.pages.create
      page2.stub!(:content).and_return("page2 content")
      page3 = page_group.pages.create
      page3.stub!(:content).and_return("page3 content")
      page_group.stub!(:pages).and_return([page1, page2, page3])
      page_group.original_content.should == "page1 content\n\n===\n\npage2 content\n\n===\n\npage3 content\n\n"
    end
end

kind of ugly.

This page_group.stub!(:pages).and_return([page1, page2, page3]) is the way I found to solve stub when have "one to many" relationship.

Is there a better way to do this, and how to refator the duplication?

 

2. params in stub

PreparationPage.stub!(:create_from_content).with("part1").and_return(page1)

你可能感兴趣的:(test spec)