svenfuchs
11/30/2008 - 1:46 PM

i wish i could write ...rb

describe "POST to :create" do
  action { post :create, @params }
  all_with :an_empty_blog
  
  it_assigns :article
  
  with :login_as_admin do
    it "succeeds", with => :valid_article_params do
      it_saves :article
      it_assigns :flash :notice => :not_nil
      it_redirects_to { url_for(@article) }
      it_pings
      it_sweeps_cache :blog, :article
    
      it "assigns the new article to the blog" do
        assigns(:article).blog.should == @blog
      end
    end
    
    it "fails", with => :invalid_article_params do
      it_does_not_save :article
      it_renders :template, :new
      it_assigns :flash, :error => :not_nil
    end
  end

  with :login_as_user, :no_login do
    it :redirects_to_login
  end
end

share :redirects_to_login do
  it_assigns :flash, :error => :not_nil
  it_redirects_to { login_path }
end
  
share :valid_article_params do
  before { @params = valid_article_params }
end

share :invalid_article_params do
  before { @params = invalid_article_params_missing_title }
  before { @params = invalid_article_params_missing_body }
end

def it_pings
  before { mock(Blog).ping }
end

def it_assigns(*names)
  assert do
    names.each { |name| assigns(name).should_not be_nil }
  end
end
before :all do
  scenario :an_empty_blog
end

test "POST to :create with an empty blog        
      with login as admin with valid article params 
      it succeeds" do
  
  lambda { login_as_admin }.call
  lambda { @params = valid_article_params }.call
  lambda { mock(Blog).ping }.call
  lambda { page_is_cached :blog, :article }.call
  
  post :create, @params
  
  assigns(:article).should_not be_nil
  assigns(:article).should_not be_new_record
  @response.flash[:notice].should_not be_nil
  assert_redirected_to url_for(@article)
  assert_page_not_cached :article
  assigns(:article).reload.blog.should == @blog
end

test "POST to :create with an empty blog 
      with login as admin with article params missing title
      it fails" do
  
  lambda { login_as_admin }.call
  lambda { @params = invalid_article_params_missing_title }.call
  
  post :create, @params
  
  assigns(:article).should_not be_nil
  assigns(:article).should be_new_record
  @response.flash[:error].should_not be_nil
  assert_template 'articles/new'
end

test "POST to :create with an empty blog  
      with login as admin with article params missing body
      it fails" do
  
  lambda { login_as_admin }.call
  lambda { @params = invalid_article_params_missing_body }.call
  
  post :create, @params
  
  assigns(:article).should_not be_nil
  assigns(:article).should be_new_record
  @response.flash[:error].should_not be_nil
  assert_template 'articles/new'
end

test "POST to :create with an empty blog 
      with login as user
      it redirects to login page" do
  
  lambda { login_as_user }.call
  
  post :create
  
  @response.flash[:error].should_not be_nil
  assert_redirected_to url_for(@article)
end

test "POST to :create with an empty blog 
      with no login
      it redirects to login page" do
  
  lambda { no_login }.call
  
  post :create
  
  @response.flash[:error].should_not be_nil
  assert_redirected_to url_for(@article)
end