- 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