- A
实例公共方法
assert_class_method(method, content, &block) 链接
断言给定类方法存在于给定内容中。此方法不检测类方法内部 (class << self),只检测以“self.”开头的类方法。当给出代码块时,它将产生方法的内容。
assert_migration "db/migrate/create_products.rb" do |migration|
assert_class_method :up, migration do |up|
assert_match(/create_table/, up)
end
end
来源:显示 | 在 GitHub 上
# File railties/lib/rails/generators/testing/assertions.rb, line 88 def assert_class_method(method, content, &block) assert_instance_method "self.#{method}", content, &block end
assert_field_default_value(attribute_type, value) 链接
断言给定的属性类型获得适当的默认值
assert_field_default_value :string, "MyString"
来源:显示 | 在 GitHub 上
# File railties/lib/rails/generators/testing/assertions.rb, line 117 def assert_field_default_value(attribute_type, value) if value.nil? assert_nil(create_generated_attribute(attribute_type).default) else assert_equal(value, create_generated_attribute(attribute_type).default) end end
assert_field_type(attribute_type, field_type) 链接
断言给定的属性类型被正确地转换为字段类型
assert_field_type :date, :date_select
来源:显示 | 在 GitHub 上
# File railties/lib/rails/generators/testing/assertions.rb, line 110 def assert_field_type(attribute_type, field_type) assert_equal(field_type, create_generated_attribute(attribute_type).field_type) end
assert_file(relative, *contents) 链接
断言给定文件存在。您需要提供一个绝对路径或相对于配置目标的路径
assert_file "config/environment.rb"
您也可以给出额外的参数。如果参数是正则表达式,它将检查正则表达式是否与给定文件内容匹配。如果它是字符串,它将比较文件与给定字符串
assert_file "config/environment.rb", /initialize/
最后,当给出代码块时,它将产生文件内容
assert_file "app/controllers/products_controller.rb" do |controller|
assert_instance_method :index, controller do |index|
assert_match(/Product\.all/, index)
end
end
来源:显示 | 在 GitHub 上
# File railties/lib/rails/generators/testing/assertions.rb, line 25 def assert_file(relative, *contents) absolute = File.expand_path(relative, destination_root) assert File.exist?(absolute), "Expected file #{relative.inspect} to exist, but does not" read = File.read(absolute) if block_given? || !contents.empty? assert_nothing_raised { yield read } if block_given? contents.each do |content| case content when String assert_equal content, read when Regexp assert_match content, read end end end
assert_instance_method(method, content) 链接
断言给定方法存在于给定内容中。当给出代码块时,它会生成方法的内容。
assert_file "app/controllers/products_controller.rb" do |controller|
assert_instance_method :index, controller do |index|
assert_match(/Product\.all/, index)
end
end
来源:显示 | 在 GitHub 上
# File railties/lib/rails/generators/testing/assertions.rb, line 100 def assert_instance_method(method, content) assert content =~ /(\s+)def #{method}(\(.+\))?(.*?)\n\1end/m, "Expected to have method #{method}" assert_nothing_raised { yield $3.strip } if block_given? end
assert_migration(relative, *contents, &block) 链接
断言给定迁移存在。您需要提供一个绝对路径或相对于配置目标的路径
assert_migration "db/migrate/create_products.rb"
此方法操作给定路径并尝试找到与迁移名称匹配的任何迁移。例如,上面的调用被转换为
assert_file "db/migrate/003_create_products.rb"
因此,assert_migration
接受与 assert_file 相同的参数。
来源:显示 | 在 GitHub 上
# File railties/lib/rails/generators/testing/assertions.rb, line 64 def assert_migration(relative, *contents, &block) file_name = migration_file_name(relative) assert file_name, "Expected migration #{relative} to exist, but was not found" assert_file file_name, *contents, &block end
assert_no_file(relative) 链接
断言给定文件不存在。您需要提供一个绝对路径或相对于配置目标的路径
assert_no_file "config/random.rb"
来源:显示 | 在 GitHub 上
# File railties/lib/rails/generators/testing/assertions.rb, line 47 def assert_no_file(relative) absolute = File.expand_path(relative, destination_root) assert !File.exist?(absolute), "Expected file #{relative.inspect} to not exist, but does" end
assert_no_migration(relative) 链接
断言给定迁移不存在。您需要提供一个绝对路径或相对于配置目标的路径
assert_no_migration "db/migrate/create_products.rb"
来源:显示 | 在 GitHub 上查看
# File railties/lib/rails/generators/testing/assertions.rb, line 74 def assert_no_migration(relative) file_name = migration_file_name(relative) assert_nil file_name, "Expected migration #{relative} to not exist, but found #{file_name}" end