I am trying to get the error flash to appear in the railstutorial.org sample app. So far this is what I have done. Can anyone help me find the error:
My RSPEC test fail:
1) SessionsController POST 'create' invalid signin should have a flash.now message
Failure/Error: flash.now[:error].should =~ /invalid/i
expected: /invalid/i开发者_运维知识库
got: nil (using =~)
./spec/controllers/sessions_controller_spec.rb:27
Controller code:
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
# Sign the user in and redirect to the user's show page.
end
end
Test code:
describe "POST 'create'" do
describe "invalid signin" do
before(:each) do
@attr = { :email => "example@gmail.com", :password => "invalid"}
end
it "should re-render the new page" do
post :create, :session => @attr
response.should render_template('new')
end
it "should have a flash.now message" do
post :create, :session => @attr
flash.now[:error].should =~ /invalid/i
end
end
It looks like in your app/controllers/sessions_controller.rb
you have a second create method declared at the bottom that just says:
def create
render 'new'
end
That second declaration is superseding your intended create method earlier in the file. Just delete those three lines (20-22 in your file) and your specs pass with flying colors, whereby I mean just one color, namely green.
精彩评论