0
這裏是RSpec的誤差:使用RSpec誤差在控制器創建
CategoriesController GET 'update' should be successful
Failure/Error: get 'update'
ActiveRecord::RecordNotFound:
Couldn't find Category without an ID
# c:in `find'
# ./app/controllers/categories_controller.rb:45:in `update'
# ./spec/controllers/categories_controller_spec.rb:35:in `block (3 levels) in <top (required)>'
這裏是在控制器的代碼:
def edit
@category = Category.find(params[:id])
end
def update
@category = Category.find(params[:id])
#@category.reload. caused nil.reload error
if @category.update_attributes(params[:category], :as => :roles_update)
@category = Category.find(params[:id])
redirect_to @category, :notice => 'Category was successfully updated'
else
@categories = Category.all
render 'index'
end
end
這裏是RSpec的代碼:
describe "GET 'update'" do
it "should be successful" do
get 'update'
response.should be_success
end
end
有什麼想法嗎?謝謝。
發佈了方法更新和編輯。將測試您的解決方案。謝謝。 – user938363
如果你要使用示例代碼我張貼,你應該改變 response.should redirect_to的(categories_path) 被更新的類路徑(這是您在更新行動在做什麼) –
仍在努力。表中有幾個類別記錄,我只是選擇其中的一個,在rspec中「更新,:id => 2,:category => {:active => false}」。仍然有ActiveRecord :: RecordNotFound:無法找到id = 2的類別。但是我可以在Rail控制檯中用Category.find(2)來取出記錄。 – user938363