我有以下Rspec测试,用于测试用户是否可以成功更新寻宝游戏。我知道该网站将让用户这样做,但我的问题是我的测试没有捕获它。每次运行rspec时,下面的所有测试都会失败。我希望只有一个简单的事情,我忘了把它放在那里,但我的眼睛很茫然,所有代码都在一起运行。当你看这些测试时,有什么事情会让你感到明显错误吗?
describe "PUT 'update'"
before(:each) do
@hunt = FactoryGirl.create(:hunt)
end
....
describe "as an admin user" do
before(:each) do
admin = FactoryGirl.create(:user, :email => "admin@example.com", :admin => true)
test_sign_in(admin)
end
....
describe "success" do
before(:each) do
@attr = { :name => "New Hunt" }
end
it "returns http success" do
get 'edit', :id => @hunt
response.should be_success
end
it "should change the hunt's name" do
put :update, :id => @hunt, :name => @attr
@hunt.reload
@hunt.name.should == @attr[:name]
end
it "should redirect to the hunt show page" do
put :update, :id => @hunt
response.should redirect_to(@hunt)
end
it "should have a flash message" do
put :update, :id => @hunt, :user => @attr
flash[:success].should =~ /updated/
end
end
...
end
这是我的控制器的代码。
def edit
@hunt = Hunt.find(params[:id])
@title = "Edit hunt"
end
def update
@hunt = Hunt.find(params[:id])
if @hunt.update_attributes(params[:hunt])
flash[:success] = "Hunt updated."
redirect_to hunts_path
else
@title = "Edit Hunt"
render 'edit'
end
end
这是我从Rspec获得的反馈。这是所有的董事会,但我希望它是由一个问题而不是四个单独的问题引起的。
1) HuntsController PUT 'update' as an admin user failure should render the 'edit' page
Failure/Error: response.should render_template('edit')
expecting <"edit"> but rendering with <"">
# ./spec/controllers/hunts_controller_spec.rb:220:in `block (5 levels) in <top (required)>'
2) HuntsController PUT 'update' as an admin user failure should have the right title
Failure/Error: response.should have_selector("title", :content => "Edit hunt")
expected following output to contain a <title>Edit hunt</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>You are being <a href="http://test.host/hunts">redirected</a>.</body></html>
# ./spec/controllers/hunts_controller_spec.rb:226:in `block (5 levels) in <top (required)>'
3) HuntsController PUT 'update' as an admin user success should change the hunt's name
Failure/Error: @hunt.name.should == @attr[:name]
expected: "New Hunt"
got: "Hunt 9" (using ==)
# ./spec/controllers/hunts_controller_spec.rb:244:in `block (5 levels) in <top (required)>'
4) HuntsController PUT 'update' as an admin user success should redirect to the hunt show page
Failure/Error: response.should redirect_to(@hunt)
Expected response to be a redirect to <http://test.host/hunts/649> but was a redirect to <http://test.host/hunts>
# ./spec/controllers/hunts_controller_spec.rb:249:in `block (5 levels) in <top (required)>'