[rspec-users] spec template for CRUD?

Chris Hoffman chris.c.hoffman at gmail.com
Fri May 4 15:17:08 EDT 2007


Hello,

So, here is my first stab at generalized CRUD behaviours.  I would
appreciate your thoughts on my approach.  Be gentle :)

describe "RUD", :shared => true do
  before(:each) do
    @model = @controller_class.to_s.sub(/Controller$/,'').singularize.constantize
    @obj = mock_model(@model)
  end

  it "should populate the object related to the model when given a valid id" do
    @model.should_receive(:find_by_id).with('1').and_return(@obj)
    get @action, :id => 1
    assigns[@model.to_s.underscore].should be(@obj)
  end

  it "should redirect_to #index when given an invalid id" do
    @model.should_receive(:find_by_id)
    get @action
    response.should redirect_to(:action => 'index')
  end

  it "should populate the flash with a message indicating that the
record wasn't found, when given an invalid id" do
    @model.should_receive(:find_by_id)
    process @action
    flash[:notice].should == "#{@model} not found"
  end
end

describe "R", :shared => true do
  it "should render the appropriate template when given a valid id" do
    @model.should_receive(:find_by_id).and_return(@obj)
    get @action
    response.should render_template(@action)
  end

  it_should_behave_like "RUD"
end

describe "show", :shared => true do
  before(:each) do
    @action = "show"
  end

  it_should_behave_like "R"
end

On 5/4/07, Chris Hoffman <chris.c.hoffman at gmail.com> wrote:
> Well, I guess posting code on blogs is fine, as long as there is a
> centralized way of accessing this code.  Perhaps some kind of
> aggregator would suffice?  I don't know if sites like technorati or
> del.icio.us can help in this respect.
>
> My initial thought was a wiki, but if you think people will want to
> control their own contributions, than obviously this is a poor idea.
>
> -Chris
>
> On 5/4/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > On 5/4/07, Chris Hoffman <chris.c.hoffman at gmail.com> wrote:
> > > Oh there is nothing wrong with the code, I just wonder if anyone has
> > > actually taken it further and implemented the behaviours required to
> > > exercise the CRUD operations in controllers (e.g., #show, #new, #edit,
> > > etc.).
> >
> > I understood that part - that you wanted to see if someone had tackled
> > the problem.
> >
> > >
> > > And as to sharing, I mean in a colloquial way, as in collaboration.
> >
> > This was the part I was talking about, and now I get it. Besides
> > posting code on blogs, what would you propose to better enable this so
> > that anybody who wants to contribute can, but everyone keeps control
> > over their own contributions?
> >
> > Cheers,
> > David
> >
> > >
> > > -Chris
> > >
> > > On 5/4/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > > > On 5/4/07, Chris Hoffman <chris.c.hoffman at gmail.com> wrote:
> > > > > Hello,
> > > > >
> > > > > Has anyone already come up with a set of shared behaviours that
> > > > > someone could leverage when adhering to a CRUD concept, with respect
> > > > > to controllers?
> > > > >
> > > > > Relatedly, it would be nice if there were a way to share generalized
> > > > > behaviour specs.
> > > >
> > > > Shared behaviours already allow you to do this, unless I'm missing
> > > > your meaning. What is it that you're looking for that shared
> > > > behaviours as/is doesn't cover?
> > > >
> > > > >
> > > > > -Chris
> > > > > _______________________________________________
> > > > > rspec-users mailing list
> > > > > rspec-users at rubyforge.org
> > > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > > >
> > > > _______________________________________________
> > > > rspec-users mailing list
> > > > rspec-users at rubyforge.org
> > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > >
> > > _______________________________________________
> > > rspec-users mailing list
> > > rspec-users at rubyforge.org
> > > http://rubyforge.org/mailman/listinfo/rspec-users
> > >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
>


More information about the rspec-users mailing list