- 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