ThingsController:
...
respond_to do |format|
format.html {redirect_to(:action => "new", :notice => "its a good thing!")} # suspect...
end
When I test this code
it "should redirect to /employees/new with a success notice" do
post :create, :emp开发者_JAVA百科loyee => Factory.build(:employee).attributes
response.should redirect_to(:action => "new")
flash[:notice].should_not be_nil
end
The test fails because it does NOT expect the actual redirection to have a notice.
Any suggestions?
Yes, I have suggestion: test different things on different lines.
response.should redirect_to(new_thing_path)
flash[:notice].should eql("its a good thing!")
This way, if the redirect_to
fails it will fail on that line, and if the flash[:notice]
is broken it will fail on that line.
Additionally, your test should work.
I know you already have an answer accepted, but I wanted to provide more insight on the issue here. The real reason your original code was failing is because this:
redirect_to(:action => "new", :notice => "its a good thing!")
Is essentially the same as this:
url = url_for(:action => "new", :notice => "its a good thing!")
redirect_to(url)
In the above, it's easier to see that the value of url
will assume that :notice
is a parameter you're trying to pass as part of the URL. What you're trying to do is pass :notice
as an option to redirect_to
, but it's being merged with your other hash options to form a URL.
Here are two (untested) solutions to try:
redirect_to({:action => "new"}, {:notice => "its a good thing!"})
or:
redirect_to(url_for(:action => "new"), :notice => "its a good thing!")
精彩评论