{"id":15203152,"url":"https://github.com/envygeeks/ruby-active_record_mocks","last_synced_at":"2025-04-13T13:15:30.465Z","repository":{"id":9826018,"uuid":"11812268","full_name":"envygeeks/ruby-active_record_mocks","owner":"envygeeks","description":"ActiveRecord Mocking for RSpec and Minitest.","archived":false,"fork":false,"pushed_at":"2016-09-26T14:22:07.000Z","size":360,"stargazers_count":3,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T13:15:25.141Z","etag":null,"topics":["activerecord","mocking","rspec","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/envygeeks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-08-01T08:52:07.000Z","updated_at":"2024-03-25T02:05:03.000Z","dependencies_parsed_at":"2022-09-06T02:01:12.101Z","dependency_job_id":null,"html_url":"https://github.com/envygeeks/ruby-active_record_mocks","commit_stats":null,"previous_names":["envygeeks/active_record_mocks"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/envygeeks%2Fruby-active_record_mocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/envygeeks%2Fruby-active_record_mocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/envygeeks%2Fruby-active_record_mocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/envygeeks%2Fruby-active_record_mocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/envygeeks","download_url":"https://codeload.github.com/envygeeks/ruby-active_record_mocks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248717238,"owners_count":21150389,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","mocking","rspec","ruby"],"created_at":"2024-09-28T04:41:28.076Z","updated_at":"2025-04-13T13:15:30.446Z","avatar_url":"https://github.com/envygeeks.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Active Record Mocks.\n\n[![Build Status](https://travis-ci.org/envygeeks/ruby-active_record_mocks.png?branch=master)](https://travis-ci.org/envygeeks/ruby-active_record_mocks) [![Coverage Status](https://coveralls.io/repos/envygeeks/ruby-active_record_mocks/badge.png?branch=master)](https://coveralls.io/r/envygeeks/ruby-active_record_mocks) [![Code Climate](https://codeclimate.com/github/envygeeks/ruby-active_record_mocks.png)](https://codeclimate.com/github/envygeeks/ruby-active_record_mocks) [![Dependency Status](https://gemnasium.com/envygeeks/ruby-active_record_mocks.png)](https://gemnasium.com/envygeeks/ruby-active_record_mocks)\n\nActiveRecord Mocks is designed to aide you in testing your ActiveRecord\nconcerns by creating random models (or even named models) that are\nremoved after each test.  It was originally concieved to test concerns,\nincludes and other types of things that normally aren't tied to a\nmodel specifically.\n\n## Installing\n\n```ruby\ngem \"active_record_mocks\"\n```\n\n## Using\n\n```ruby\nwith_mocked_tables do |m|\n  m.enable_extension \"uuid-ossp\"\n  m.enable_extension \"hstore\"\n\n  t1 = m.create_table do |t|\n    t.model_name :Foo\n    t.belongs_to :bar\n\n    t.layout do |l|\n       l.integer :bar_id\n    end\n  end\n\n  t2 = m.create_table do |t|\n    t.model_name :Bar\n    t.has_many   :foo\n\n    t.layout do |l|\n      l.text :bar_text\n    end\n  end\n\n  # Do Work Here\nend\n```\n\n---\n\n### Extensions\n\nYou can enable PostgreSQL extensions inside of your models using the\n`enable_extension` method when inside of `with_mocked_tables` or\n`with_mocked_models` like so:\n\n```ruby\nwith_mocked_tables do |m|\n  m.enable_extension \"extension-name\"\nend\n```\n\n---\n\n### Creating Tables and Layouts\n\nTo create tables you use the `create_table` method when inside of\n`with_mocked_tables` or `with_mocked_models`, like so:\n\n```ruby\nwith_mocked_tables do |m|\n  m.create_table migration_arguments do |t|\n    t.layout do |l|\n      l.text :foo_text\n    end\n  end\nend\n```\n\n#### Belongs to, Has Many and other methods\n\nAny method that `ActiveRecordMocks` does not know or understand is\npassed on to the model itself, so if you need for example `belongs_to`\nthen you would simply use belongs to when creating your table:\n\n```ruby\nwith_mocked_tables do |m|\n  m.create_table migration_arguments do |t|\n    t.belongs_to :bar_model\n    t.layout do |l|\n      l.text :foo_text\n    end\n  end\nend\n```\n\n#### Named models and tables\n\nIf you need a named model or a named table or a model whose table is\ndifferent than it's model you can use the methods `model_name` and\n`table_name`, if you simply need a named model and you use standard\nnaming conventions than you can simply leave out the `table_name`\nwhen using model name and `ActiveRecordMocks` will tabelize the name\nof your model automatically the same as `Rails` would.\n\n```ruby\nwith_mocked_tables do |m|\n  t1 = m.create_table migration_arguments do |t|\n    t.model_name :Foo\n    t.layout do |l|\n      l.text :foo_text\n    end\n  end\nend\n\n# Results in:\n#   - Foo  (Model)\n#   - foos (Table)\n```\n\n```ruby\nwith_mocked_tables do |m|\n  t1 = m.create_table migration_arguments do |t|\n    t.table_name :old_foo\n    t.model_name :Foo\n    t.layout do |l|\n      l.text :foo_text\n    end\n  end\nend\n\n# Results in:\n#   - Foo      (Model)\n#   - old_foo  (Table)\n```\n\n#### Model Includes\n\nIf you need to include anything into your model you can use the\n`includes` method when inside of `with_mocked_models` or\n`with_mocked_tables`, like so:\n\n```ruby\nwith_mocked_tables do |m|\n  m.create_table migration_arguments do |t|\n    t.includes Bar1, Bar2\n    t.layout do |l|\n      l.text :foo_text\n    end\n  end\nend\n```\n\n#### Using a custom parent class\n\nIf you need to test a base class that is not ActiveRecord::Base,\nyou can do so by specifying the `parent_class` method.\n\nThis is useful if your code base uses a custom base class that\nderives from ActiveRecord::Base, like so:\n\n```ruby\n\nclass MyBase \u003c ActiveRecord::Base\n  self.abstract_class = true\n  def a_custom_method\n    42\n  end\nend\n\nwith_mocked_tables do |m|\n  m.create_table migration_arguments do |t|\n    t.parent_class :MyBase\n    t.model_name :Foo\n    t.layout do |l|\n      l.text :foo_text\n    end\n  end\n\n  f = Foo.new\n  f.is_a?(MyBase)   # \u003c= true\n  f.a_custom_method # \u003c= 42\nend\n\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenvygeeks%2Fruby-active_record_mocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenvygeeks%2Fruby-active_record_mocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenvygeeks%2Fruby-active_record_mocks/lists"}