- 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_initializer(name, *contents, &block) 链接
断言给定的初始化程序存在。您需要提供相对于“config/initializers/”目录的路径。
assert_initializer "mail_interceptors.rb"
您还可以提供其他参数。如果参数是正则表达式,它将检查正则表达式是否与给定的文件内容匹配。如果它是字符串,它会比较文件和给定的字符串
assert_initializer "mail_interceptors.rb", /SandboxEmailInterceptor/
最后,当给出块时,它会屈服于文件内容
assert_initializer "mail_interceptors.rb" do |initializer| assert_match(/SandboxEmailInterceptor/, initializer) end
来源: 显示 | 在 GitHub 上
# File railties/lib/rails/generators/testing/assertions.rb, line 141 def assert_initializer(name, *contents, &block) assert_file("config/initializers/#{name}", *contents, &block) 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