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.
1) 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.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/ouy4grWHNOwJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Are u using mocha? If you are using mocha, I think you should write
something like that:
mock(''post'', hash)
regards
2012/5/23 Kleber Shimabuku
<klebershimabuku-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> 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.
>
> 1) 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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rubyonrails-talk/-/ouy4grWHNOwJ.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
--
thiagocifani
http://thiagocifani.wordpress.com/
twitter.com/thiagocifani
del.icio.us/thiagocifani
<http://del.icio.us/thiagocifani>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Guru Cifani lol 2012/5/23 thiagocifani <cifani.thiago-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> Are u using mocha? If you are using mocha, I think you should write > something like that: > > mock(''post'', hash) > > > regards > > > 2012/5/23 Kleber Shimabuku <klebershimabuku-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > >> 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. >> >> 1) 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. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/ouy4grWHNOwJ. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > > > -- > thiagocifani > http://thiagocifani.wordpress.com/ > twitter.com/thiagocifani > del.icio.us/thiagocifani > <http://del.icio.us/thiagocifani> > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- att, Rogerio A complicação se descomplica na mesma proporção que fazemos os nós se desatarem ao tecer o conhecimento do saber. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I assume you are trying to mock the Post to separate it from the controller action. You may have better luck with the double/stub nomenclature: https://github.com/rspec/rspec-mocks/blob/master/README.md http://rubydoc.info/gems/rspec-mocks/frames On Wednesday, May 23, 2012 10:51:38 AM UTC-7, Kleber Shimabuku wrote:> > 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. > > 1) 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. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/13CxFWLAGgIJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi,
I''ve changed my test a little bit so the only error I getting is the
following one:
Failures:
1) PostsController#create when logged in failure should render the NEW
template
Failure/Error: response.should render_template :new
expecting <"new"> but rendering with <"">
# ./spec/controllers/posts_controller_spec.rb:70:in `block (5 levels)
in <top (required)>''
Finished in 1.27 seconds
7 examples, 1 failure
And the modified test:
describe ''#create'' do
@attributes = { post: { id: 1, title: ''New post'',
description:
''Please add some text here'', location:
''Anywhere'' } }
context ''when logged in'' do
before do
@user = FactoryGirl.create(:user, email:
''random-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'')
sign_in @user
@post = mock(Post,
title: ''New post'',
description: ''Please add some text
here'',
location: ''Anywhere'')
# expected
controller.current_user.posts.stub!(:build).and_return(@post)
end
context ''success'' do
before { @post.should_receive(:save).and_return(true) }
it ''should create a instance variable'' do
post :create, @attributes
assigns(:post).title.should be_eql ''New post''
end
it ''should redirect to the success page'' do
post :create, @attributes
response.should redirect_to successful_submitted_posts_path
end
end
context ''failure'' do
before { @post.should_receive(:save).and_return(false) }
it ''should render the NEW template'' do
post :create, { post: { id: 1, title: ''New post'',
description:
''Please add some text here'', location:
''Anywhere'' } }
response.should render_template :new
end
end
end
I''ve been googling for this yesterday and it seems that I need to add
somewhere an error hash, so it will render the new template instead of
doing a redirect.
Em quinta-feira, 24 de maio de 2012 22h33min46s UTC-3, Tyler
escreveu:>
> I assume you are trying to mock the Post to separate it from the
> controller action. You may have better luck with the double/stub
> nomenclature:
>
> https://github.com/rspec/rspec-mocks/blob/master/README.md
>
> http://rubydoc.info/gems/rspec-mocks/frames
>
>
>
> On Wednesday, May 23, 2012 10:51:38 AM UTC-7, Kleber Shimabuku wrote:
>>
>> 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.
>>
>> 1) 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.
>>
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/7TfbF7sRwDwJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Anyone? Em quarta-feira, 23 de maio de 2012 14h51min38s UTC-3, Kleber Shimabuku escreveu:> > 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. > > 1) 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. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/nN28v1WujRwJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Have you tried with the double/stub nomenclature as I suggested? On Saturday, May 26, 2012 9:02:46 PM UTC-7, Kleber Shimabuku wrote:> > Anyone? > > > > Em quarta-feira, 23 de maio de 2012 14h51min38s UTC-3, Kleber Shimabuku > escreveu: >> >> 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. >> >> 1) 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. >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ppfNSs_t6r0J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.