1

I have written this controller code in Ruby on Rails

class PostsController < ApplicationController
  before_filter :authenticate_user!

  def index  
    @posts = Post.all(:order => "created_at DESC")  
    respond_to do |format|  
      format.html  
    end  
  end  

  def create  
    @post = Post.create(:message => params[:message])  
    respond_to do |format|  
      if @post.save  
        format.html { redirect_to posts_path }  
        format.js
      else  
        flash[:notice] = "Message failed to save."  
        format.html { redirect_to posts_path }  
      end  
    end  
  end  
end

and corresponding to this I have written the following test case :-

require 'spec_helper'

describe PostsController do
  describe "GET 'index'" do
    it "returns http success" do
      get 'index'
      response.should be_success
    end
  end

  describe "#create" do
    it "creates a successful mesaage post" do
      @post = Post.create(message: "Message")
      @post.should be_an_instance_of Post
    end
  end
end

I am getting failures on both. Please take a look on the code and help me figure out.

1
  • 1
    Could you include the error messages? Commented Jul 3, 2012 at 8:17

3 Answers 3

5

I suspect you are not logged in since you are using Devise?

Maybe you need to include the devise testhelpers:

describe PostsController do
  include Devise::TestHelpers
  before(:each) do
    @user = User.create(...)
    sign_in @user
  end

  #assertions go here
end
Sign up to request clarification or add additional context in comments.

Comments

1

As Tigraine states, it appears as though you probably are not logged in (with Devise) when the tests get executed. However, showing the failures would help in narrowing down the problem further.

On top of that, the second test isn't really an integration test and I would probably prefer something like the following to test the same condition. There are two types of test you could do:

# inside 'describe "#create"'

let(:valid_params) { {'post' => {'title' => 'Test Post'} }

it 'creates a new Post' do
  expect {
    post :create, valid_params
  }.to change(Post, :count).by(1)
end

# and / or

it 'assigns a new Post' do
  post :create, valid_params
  assigns(:post).should be_a(Post)
  assigns(:post).should be_persisted
end

2 Comments

It says, NoMethodError: undefined method `symbolize_keys' for "valid_params":String
Sorry, valid_params should be a hash mirroring a valid set of attributes for your Post model e.g. { 'post' => { 'name' => 'Test' } }
0

Don't forget to add this line into your spec_helper.rb

require "devise/test_helpers"
include Devise::TestHelpers

Nevertheless, here is link for Devise wiki - How to test Controllers where you can find more info about this approach. I recommend writing the before method without (:each), what I remember it sometimes causes problems.

before do
 @user = FactoryGirl.create(:user)
 sign_in @user
end

Can always use:

puts response.inspect

To see how your response looks like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.