Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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内部验证引用的常量_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails 如何覆盖从RSpec内部验证引用的常量

Ruby on rails 如何覆盖从RSpec内部验证引用的常量,ruby-on-rails,rspec,Ruby On Rails,Rspec,我的模型有这样的自定义验证 class User # skip before MAX_FILE_SIZE = 10.megabytes.to_i validates :file, size: { max: MAX_FILE_SIZE } # skip after end context "MAX_FILE_SIZE is default" do it do # test something end end context "MAX_FILE_SIZE is

我的模型有这样的自定义验证

class User
  # skip before

  MAX_FILE_SIZE = 10.megabytes.to_i
  validates :file, size: { max: MAX_FILE_SIZE }

  # skip after
end
context "MAX_FILE_SIZE is default" do
  it do
    # test something
  end
end
context "MAX_FILE_SIZE is 1byte" do
  before do
    stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
  end
  it do
    # test something
  end
end
context "MAX_FILE_SIZE is 1byte" do
  before do
    stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
    load "user.rb"
  end
  it do
    # test something
  end
end
class User
  # skip before

  MAX_FILE_SIZE ||= 10.megabytes.to_i
  validates :file, size: { max: MAX_FILE_SIZE }

  # skip after
end  
我想通过重写规范中的
MAX\u FILE\u SIZE
来测试
MAX\u FILE\u SIZE
。所以,我这样写

class User
  # skip before

  MAX_FILE_SIZE = 10.megabytes.to_i
  validates :file, size: { max: MAX_FILE_SIZE }

  # skip after
end
context "MAX_FILE_SIZE is default" do
  it do
    # test something
  end
end
context "MAX_FILE_SIZE is 1byte" do
  before do
    stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
  end
  it do
    # test something
  end
end
context "MAX_FILE_SIZE is 1byte" do
  before do
    stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
    load "user.rb"
  end
  it do
    # test something
  end
end
class User
  # skip before

  MAX_FILE_SIZE ||= 10.megabytes.to_i
  validates :file, size: { max: MAX_FILE_SIZE }

  # skip after
end  
第一次测试通过了。但是,由于未更改最大文件大小,第二次测试失败


你能告诉我为什么吗?以及如何解决这种情况。

文档中说,在示例运行后,常量值会重置

您是否尝试在示例中删除常量

context "MAX_FILE_SIZE is 1byte" do
  it do
    stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
    # test something
  end
end

我找到了原因

由于
config/environments/test.rb
中的
config.cache\u classes=true,我的自定义验证器只创建了一次。更改的常量值对验证器的实例无效

我不知道最好的解决办法

下面的不是一个好的解决方案

  • config/environments/test.rb
    中设置
    config.cache\u classes=false
  • 在设置常量值存根后重新加载模型类,如下所示

    class User
      # skip before
    
      MAX_FILE_SIZE = 10.megabytes.to_i
      validates :file, size: { max: MAX_FILE_SIZE }
    
      # skip after
    end
    
    context "MAX_FILE_SIZE is default" do
      it do
        # test something
      end
    end
    context "MAX_FILE_SIZE is 1byte" do
      before do
        stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
      end
      it do
        # test something
      end
    end
    
    context "MAX_FILE_SIZE is 1byte" do
      before do
        stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
        load "user.rb"
      end
      it do
        # test something
      end
    end
    
    class User
      # skip before
    
      MAX_FILE_SIZE ||= 10.megabytes.to_i
      validates :file, size: { max: MAX_FILE_SIZE }
    
      # skip after
    end  
    
  • 模型类中禁止更改常量值,如下所示

    class User
      # skip before
    
      MAX_FILE_SIZE = 10.megabytes.to_i
      validates :file, size: { max: MAX_FILE_SIZE }
    
      # skip after
    end
    
    context "MAX_FILE_SIZE is default" do
      it do
        # test something
      end
    end
    context "MAX_FILE_SIZE is 1byte" do
      before do
        stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
      end
      it do
        # test something
      end
    end
    
    context "MAX_FILE_SIZE is 1byte" do
      before do
        stub_const "User::MAX_FILE_SIZE", 1.bytes.to_i
        load "user.rb"
      end
      it do
        # test something
      end
    end
    
    class User
      # skip before
    
      MAX_FILE_SIZE ||= 10.megabytes.to_i
      validates :file, size: { max: MAX_FILE_SIZE }
    
      # skip after
    end  
    

  • 谢谢上述案件将获得通过。实际上,我的规范是rspec rails的请求规范。这个常数似乎改变了。但是,第二个例子失败了。