【问题标题】:rspec error with create in controller在控制器中创建 rspec 错误
【发布时间】:2011-09-21 04:02:39
【问题描述】:

这是 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

有什么想法吗?谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 rspec2


    【解决方案1】:

    您粘贴了创建操作而不是更新操作。此外,您正在尝试使用 get 请求测试更新操作。如果您遵循约定,它应该使用 put 请求。

    如果你已经实现了更新操作......你会或多或少地测试:

    describe CategoriesController do
      let(:category) { mock_model(Category).as_null_object }
    
      describe "PUT update" do
        before do
          Category.should_receive(:find).with(5).and_return(category)
        end
    
        context "when a category updates succesfully" do
          before do
            category.stub(:update_attributes).and_return(true)
          end
    
          it "redirects to the categories page" do
            put :update, :id => 5, :category => { :some_val => 4 }
            response.should redirect_to(categories_path)
          end
    
          it "sets the flash message" do
            put :update, :id => 5, :category => { :some_val => 4 }
            flash[:notice].should eq("Category was succesfully updated")
          end
        end
    
        context "when a category does not update successfully" do
          before do
            category.stub(:update_attributes).and_return(false)
          end
    
          it "sets the flash message"
          it "redirects to the categories page"
    
          # etc etc
        end
      end
    end
    

    要达到这一点(意味着添加模拟模型、存根,你有什么),你通常会开始“新鲜”,可以这么说,并按照 TDD 风格工作。希望对你有帮助

    【讨论】:

    • 发布了方法更新和编辑。将测试您的解决方案。谢谢。
    • 如果您要使用我发布的示例代码,您应该将 response.should redirect_to(categories_path) 更改为正在更新的类别的路径(这是您在更新操作中所做的)
    • 仍在尝试。表中有一些类别记录,我只选择其中一个来“将 update, :id => 2, :category => {:active => false} 放入 rspec。仍然有 ActiveRecord::RecordNotFound:Couldn't find Category with id=2。但是我可以在 rails 控制台中使用 Category.find(2) 提取记录。
    • 因为你是在测试环境下运行的,你的记录只存在于开发环境中。在这种情况下,您传递的 ID 为 2。您需要存根 find 方法并返回一个模拟对象。您实际上并没有从数据库中检索记录,rspec 阻止它并提供模拟。在代码示例中,我指示 rspec 返回一个模拟对象,但我强制必须使用 ID 5 调用 find 方法。要运行 ID 2 的测试,只需替换为:Category.should_receive(:find)。 with(2).and_return(类别)
    • 我建议您阅读文章和示例,也许是有关使用 rspec 等库进行测试的书籍。这个特定的示例代码将在达到所需功能的最后,例如......更新操作。整个过程和测试点远不止于此。你经历了 TDD 周期,当你在整个红色周期中变成绿色时,你开始模拟,这使得你的测试运行得更快一些。
    猜你喜欢
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多