Dont know how to write test cases

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Dont know how to write test cases

a-iparikh
Hi all...
 I am new to rspec. I am testing create action of communities_controller
in my application, which is below:
def create
    @community = Community.new(params[:community])
    @community.url_title = params[:community][:title].parameterize
    @community.title =  params[:community][:title].strip
    @community.description =  params[:community][:description].strip
    @community.country_id = params[:referenceid][:country]
    @community.state_id = params[:referenceid][:state]
    @community.city_id = params[:referenceid][:city]
    @community.user_id = current_user.id
    respond_to do |format|
      if @community.save
        format.html { redirect_to :action => "show", :id =>
@community.url_title}
        format.xml  { render :xml => @community, :status => :created,
:location => @community }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @community.errors, :status =>
:unprocessable_entity }
      end
    end
  end
My test cases are as below:

describe "POST create" do
    describe "with valid params" do
      it "assigns a newly created community as @community" do
  Community.stub(:new).with({'these' => 'params'}) {
mock_community(:save => true) }
  post :create, :community => {'these' => 'params'}
        assigns(:community).should be(mock_community)
      end

      it "redirects to the created community" do
        Community.stub(:new) { mock_community(:save => true) }
        post :create, :community => {}
        response.should redirect_to(community_url(mock_community))
      end
    end

    describe "with invalid params" do
      it "assigns a newly created but unsaved community as @community"
do
        Community.stub(:new).with({'these' => 'params'}) {
mock_community(:save => false) }
        post :create, :community => {'these' => 'params'}
        assigns(:community).should be(mock_community)
      end

      it "re-renders the 'new' template" do
        Community.stub(:new) { mock_community(:save => false) }
        post :create, :community => {}
        response.should render_template("new")
      end
    end
  end

I dont know how to test : @community.url_title =
params[:community][:title].parameterize
I am having following failure:
Failure/Error: post :create, :community => {'these' => 'params'}
    undefined method `parameterize' for nil:NilClass
Please help me...