Basic Controller Rspecs

Suppose the following method is there in the controller

def new
  @post = Post.new
end

For this the corresponding spec will be as follows

describe "GET #new" do
  it "creates new instance of post" do
    get :new
    assigns(:post).should be_a_new(Post)
  end
end

In the above code “get :new” line will get the new path of the controller and execute the corresponding method described in the controller. So that particular statement is very important for execution of the method and comparison of the output in the test.

Second line indicates what is expected that particular method to do, since in that method a new instance of the Post model is expected to be assigned to a post variable, that is indicated in it.
Suppose the following method is there in the controller

def index
  @posts = Post.all
end

For this the corresponding spec will be as follows

describe "GET #index" do
  it "list all posts" do
    @post= FactoryGirl.create(:post)
    get :index
    assigns(:posts).should eq([@post])
   end
end

In this method for posts variable array of all posts are expected to be assigned, so it is written as ‘[@post]’.
Suppose the following method is there in the controller

def update
  @post = Post.find(params[:id])
  @post.update_attributes(params[:post])
  redirect_to posts_url, notice: 'Post was successfully updated.'
end

For this the corresponding spec will be as follows

describe "PUT #update" do
  it "updates the particular post" do
    @post = FactoryGirl.create(:post)
    put :update, {:id => @post, :post => {:title =>'new',:body=>'content of the post'}}
    response.should redirect_to posts_url
    flash[:notice].should eql("Post was successfully updated.")
  end
end

In the above code First line
@post=FactoryGirl.create(:post)
the line of code is creating an instance of the object using FactoryGirl.
The update method expects two parameters when it is supposed to be executed, which are params[:id] and params[:post].
put :update, {:id => @post, :post => {:title =>’new’,:body=>’content of the post’}}’
So the above line of code is to execute the update method defined in the controller along with the expected parameters.
After successful execution of the method, it responds with a redirect request and flash[:notice].

Suppose the following method is there in the controller

def authentication
  if user_signed_in?
    render :partial => "authorised"
  else
    render :partial => "login"
  end
end

For this the corresponding spec will be as follows

describe "GET #authentication" do
  it "renders authorized partial if user signed in" do
    @user = FactoryGirl.create(:user)
    sign_in @user
    get :authentication
    response.should render_template('_authorised')
  end
  it "renders login partial if user not signed in" do
    get :authentication
    response.should render_template('_login')
  end
end

In the above method based on whether user is signed in or not the partial is rendered and it is verified in the spec using ‘render_template’.

Suppose the following method is there in the controller

def is_post_exist
  post = Post.find_by_title(params[:title])
  render :json => {status: 200, result: post.nil?}
end

For this the corresponding spec will be as follows

describe "GET #is_post_exist" do
  it "returns false if post exists" do
    @post = FactoryGirl.create(:post)
    @expected = {
      :status => 200,
      :result => @post.nil?
    }.to_json
    get :is_post_exist, {:title => @post.title}
    response.body.should == @expected
  end
end

In the above method a json repsone is expected, so in this case a @expected variable is created with the paramaters and is converted to json using ‘to_json’ method. And then reponse.body is checked with it.

These are few general methods used to controller specs.

About these ads

2 thoughts on “Basic Controller Rspecs

Comments are closed.