Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec+;工厂女孩测试404 ActiveRecord::RecordNotFound_Ruby On Rails_Rspec_Ruby On Rails 3.2_Factory Bot - Fatal编程技术网

Ruby on rails RSpec+;工厂女孩测试404 ActiveRecord::RecordNotFound

Ruby on rails RSpec+;工厂女孩测试404 ActiveRecord::RecordNotFound,ruby-on-rails,rspec,ruby-on-rails-3.2,factory-bot,Ruby On Rails,Rspec,Ruby On Rails 3.2,Factory Bot,我的控制器规范文件中有这个 it "should raise 404" do business = FactoryGirl.build(:business) expect{get :edit, :id => business}.to raise_error(ActiveRecord::RecordNotFound) end 如果我是对的,构建不会保存到数据库,因此业务不应该存在,我的测试应该通过,但它不会 我还尝试使用字符串作为“id”的值,但仍然失败 我

我的控制器规范文件中有这个

it "should raise 404" do
      business = FactoryGirl.build(:business)
      expect{get :edit, :id => business}.to raise_error(ActiveRecord::RecordNotFound)
    end
如果我是对的,构建不会保存到数据库,因此业务不应该存在,我的测试应该通过,但它不会

我还尝试使用字符串作为“id”的值,但仍然失败

我已尝试此控制器操作:

def edit
    if params[:id].to_i == 0
      name = params[:id].to_s.titleize
      @business = Business.find_by_name!(name)
    else
      @business = Business.find(params[:id])
    end
    respond_with(@business)
  end
一个不存在的ID,它确实显示了404

如果您询问为什么会出现这样的情况,我也会让此操作响应“id”参数的字符串

应用程序控制器中的以下代码接收任何ActiveRecord::RecordNotFound:

rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

  private
    def record_not_found
      render :text => "404 Not Found Baby!", :status => 404
    end

为什么我的404测试没有通过?

您的控制器没有引发
ActiveRecord::RecordNotFound
异常,它在ApplicationController中从中解救出来。所以试着测试响应代码或文本,比如

  it "should respond with a 404" do
    business = FactoryGirl.build(:business)
    get :edit, :id => business
    response.response_code.should == 404
  end

您的控制器没有引发
ActiveRecord::RecordNotFound
异常,它在ApplicationController中从中解救。所以试着测试响应代码或文本,比如

  it "should respond with a 404" do
    business = FactoryGirl.build(:business)
    get :edit, :id => business
    response.response_code.should == 404
  end

我知道我来晚了,但你不应该真的在控制器测试中创建记录。您可以在模型测试中创建记录

在控制器测试中,如果您希望创建失败,请使用类似于
my\u model.stub(:save.)和\u return(false)
的内容对其进行存根。如果希望创建成功,可以使用
my\u model.stub(:save.)和\u return(true)

使用Shoulda

context "record valid" do
  before :each do
    my_model.stub(:save).and_return(true)
    post :create
  end
  it { should redirect_to(dashboard_url) }
end

我知道我来晚了,但你不应该真的在控制器测试中创建记录。您可以在模型测试中创建记录

在控制器测试中,如果您希望创建失败,请使用类似于
my\u model.stub(:save.)和\u return(false)
的内容对其进行存根。如果希望创建成功,可以使用
my\u model.stub(:save.)和\u return(true)

使用Shoulda

context "record valid" do
  before :each do
    my_model.stub(:save).and_return(true)
    post :create
  end
  it { should redirect_to(dashboard_url) }
end

尝试了您的答案,结果也是:response.status.should是(404),但仍然失败您是否有针对ActionController::RoutingError的任何补救措施?我猜您应该不会得到路由错误,因为:id=nil。尝试get:edit,:id=>999,这应该可以工作;手动ID应该可以工作,factorygirl的构建方法似乎代表了一个现有的object@ants建议当然值得考虑,但下面是一些关于上述示例的评论,可能会对未来的读者有所帮助:
FactoryGirl.build
创建对象,但不会持久化它。控制器正在查询您尚未保存的对象,而该对象似乎并不完全是您在此处想要的…*如果您有一个持久化对象,那么这可能会更好:
get:edit,:id=>business.id
(即使rails为您提取id,使用隐式测试数据也会使您的规范更清晰)…*由于您正在测试一个不存在的记录,我将完全跳过构建工厂…尝试了您的答案,这个答案也是:response.status.should(404),但仍然失败您是否有针对ActionController::RoutingError的任何补救措施?我猜您应该不会得到路由错误,因为:id=nil。尝试get:edit,:id=>999,这应该可以工作;手动ID应该可以工作,factorygirl的构建方法似乎代表了一个现有的object@ants建议当然值得考虑,但下面是一些关于上述示例的评论,可能会对未来的读者有所帮助:
FactoryGirl.build
创建对象,但不会持久化它。控制器正在查询您尚未保存的对象,而该对象似乎并不完全是您在此处想要的…*如果您有一个持久化对象,那么这可能会更好:
get:edit,:id=>business.id
(即使rails为您提取id,使用隐式测试数据也会使您的规范更清晰)…*由于您正在测试一个不存在的记录,我将完全跳过构建工厂…您是否尝试在其中插入断点以查看控制器正在执行的操作?您是否尝试在其中插入断点以查看控制器正在执行的操作?