Hi guys,
I’m starting to write tests and I writing a functional test for my ‘create’ action.
So, basically I have the following test:
describe ‘#create’ do
@attributes = { title: ‘New post’, description: ‘Please add some text here’, location: ‘Anywhere’ }
context ‘when logged in’ do
login_user
before { post :create, @attributes }
it ‘should create a new post’ do
post = mock(Post, @attributes)
assigns(:post).should_not be_nil
Post.should_receive(:save).and_return(post)
response.should redirect_to successful_submitted_posts_path
end
it ‘should NOT create a new post’ do
end
end
context ‘when NOT logged in’ do
before { post :create, @attributes }
it { response.should_not be_successful }
it { response.should redirect_to new_user_session_path }
end
end
But it’s failing and I don’t get understand why.
- PostsController#create when logged in should create a new post
Failure/Error: post = mock(Post, @attributes)
ArgumentError:
wrong number of arguments (3 for 2)
./spec/controllers/posts_controller_spec.rb:40:in `block (4 levels) in <top (required)>’
Finished in 0.77996 seconds
6 examples, 1 failure
why mock method expects 3 arguments? what they should be?
I have read on some blogs this kind of syntax.
mock(Object, hash)
And this is my controller:
encoding: utf-8
class PostsController < ApplicationController
load_and_authorize_resource
before_filter :authenticate_user!
respond_to :html
def new
@post = Post.new
end
def create
@post = current_user.posts.build(params[:post])
if @post.save
redirect_to successful_submitted_posts_path
else
render :new
end
end
def successful_submitted; end
end
What I’m doing wrong?
Please give me some hints.
Thank you.