Gordon
2012-Mar-26 10:47 UTC
[rspec-users] ''new'' method works as expected but rspec does not recognise flash contents or redirection
hi all :)
Found a spec failing today.
Not sure why.
When I run my application and successfully added a new object, i can
see
1) a redirection has taken place to the index page as expected (302
code returned) from the console
2) the flash notice''s message being displayed in the index page
BUT
when I run the specs (see below), it fails indicating that the
redirection did not occur (as a 200 code was returned) AND
that the flash notice message was nil.
---------specs error start -------------------------------------
1) PartsController saves the new part object successfully sets the
flash with a success message
Failure/Error: flash[:notice].should eq(''Part was successfully
created.'')
expected: "Part was successfully created."
got: nil
(compared using ==)
# ./spec/controllers/parts_controller_spec.rb:35:in `block (3
levels) in <top (required)>''
2) PartsController saves the new part object successfully redirects
the user back to the parts index page
Failure/Error: response.should redirect_to( :action =>
''index'' )
Expected response to be a <:redirect>, but was <200>
# ./spec/controllers/parts_controller_spec.rb:39:in `block (3
levels) in <top (required)>''
--------- specs error end -------------------------------------
--------- extract of spec/controllers/parts_controller_spec.rb - start
------------------
context ''saves the new part object successfully'' do
before(:each) do
post :create, :part => { ''title'' =>
''HKS boost
controller'' }
end
# we could combine the 2 specs below to save on the post
request
# to the create action but having 2 separate specs would give
# clarity
it ''sets the flash with a success message'' do
flash[:notice].should eq(''Part was successfully
created.'')
end
it ''redirects the user back to the parts index page''
do
response.should redirect_to( :action => ''index''
)
end
end
--------- extract of spec/controllers/parts_controller_spec.rb - end
------------------
I''m not sure what''s going on :(
-------- "parts_controller.rb" - start
-------------------------------
# POST /parts
# POST /parts.xml
def create
# Record current user''s id as he/she created the part
params[:part][:created_by] = current_user.id
params[:part][:updated_by] = current_user.id
@part = Part.new(params[:part])
respond_to do |format|
if @part.save
flash[:notice] = ''Part was successfully created.''
format.html { redirect_to(parts_path) }
format.xml { render :xml => @part, :status
=> :created, :location => @part }
format.json {
@parts = Part.all;
render :json => @parts
}
else
format.html { render :action => "new" }
format.xml { render :xml => @part.errors, :status
=> :unprocessable_entity }
end
end
end
-------- "parts_controller.rb" - end -------------------------------
Gordon Yeong
2012-Mar-26 10:58 UTC
[rspec-users] ''new'' method works as expected but rspec does not recognise flash contents or redirection
Guys, Not sure why but I deleted my Gemfile.lock file. Found that it''s due to a stale Gemfile.lock file which is still using rspec-core 2.8.0 when in fact I just performed and update yesterday night on rspec (hence I''ve got rspec-core 2.9.0). Ran "rspec parts_controller.spec.rb" again and all tests are passing. Could it really be the stale Gemfile.lock file? hmmm On 26 March 2012 21:47, Gordon <anexiole at gmail.com> wrote:> hi all :) > > Found a spec failing today. > Not sure why. > > When I run my application and successfully added a new object, i can > see > 1) a redirection has taken place to the index page as expected (302 > code returned) from the console > 2) the flash notice''s message being displayed in the index page > > BUT > when I run the specs (see below), it fails indicating that the > redirection did not occur (as a 200 code was returned) AND > that the flash notice message was nil. > > > ---------specs error start ------------------------------------- > > 1) PartsController saves the new part object successfully sets the > flash with a success message > Failure/Error: flash[:notice].should eq(''Part was successfully > created.'') > > expected: "Part was successfully created." > got: nil > > (compared using ==) > # ./spec/controllers/parts_controller_spec.rb:35:in `block (3 > levels) in <top (required)>'' > > 2) PartsController saves the new part object successfully redirects > the user back to the parts index page > Failure/Error: response.should redirect_to( :action => ''index'' ) > Expected response to be a <:redirect>, but was <200> > # ./spec/controllers/parts_controller_spec.rb:39:in `block (3 > levels) in <top (required)>'' > > --------- specs error end ------------------------------------- > > --------- extract of spec/controllers/parts_controller_spec.rb - start > ------------------ > > context ''saves the new part object successfully'' do > before(:each) do > post :create, :part => { ''title'' => ''HKS boost > controller'' } > end > > # we could combine the 2 specs below to save on the post > request > # to the create action but having 2 separate specs would give > # clarity > it ''sets the flash with a success message'' do > flash[:notice].should eq(''Part was successfully created.'') > end > > it ''redirects the user back to the parts index page'' do > response.should redirect_to( :action => ''index'' ) > end > end > > --------- extract of spec/controllers/parts_controller_spec.rb - end > ------------------ > > > I''m not sure what''s going on :( > > > > > -------- "parts_controller.rb" - start > ------------------------------- > > # POST /parts > # POST /parts.xml > def create > # Record current user''s id as he/she created the part > params[:part][:created_by] = current_user.id > params[:part][:updated_by] = current_user.id > > @part = Part.new(params[:part]) > respond_to do |format| > if @part.save > flash[:notice] = ''Part was successfully created.'' > format.html { redirect_to(parts_path) } > format.xml { render :xml => @part, :status > => :created, :location => @part } > format.json { > @parts = Part.all; > render :json => @parts > } > else > format.html { render :action => "new" } > format.xml { render :xml => @part.errors, :status > => :unprocessable_entity } > end > end > end > > > -------- "parts_controller.rb" - end ------------------------------- > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120326/37c7372a/attachment-0001.html>