|
1 | | -# RSpec::ActiveModel::Mocks [](http://travis-ci.org/rspec/rspec-activemodel-mocks) |
| 1 | +# RSpec::ActiveModel::Mocks |
2 | 2 |
|
3 | | -RSpec::ActiveModel::Mocks provides tools for testing `ActiveModel` classes. |
4 | | - |
5 | | - mock_model(Person, name: "Fred") |
6 | | - |
7 | | -## Install |
8 | | - |
9 | | -Add this line to your application's gemfile: |
10 | | - |
11 | | - gem 'rspec-activemodel-mocks' |
12 | | - |
13 | | -And then execute: |
14 | | - |
15 | | - $ bundle |
16 | | - |
17 | | -If you are using [rspec-rails](https://github.com/rspec/rspec-rails) and have |
18 | | -followed the installation instructions there, you're all set to use `stub_model` |
19 | | -and `mock_model`. |
20 | | - |
21 | | -To use `stub_model` and `mock_model` without rspec-rails, require the |
22 | | -following file: |
23 | | - |
24 | | - require 'rspec/active_model/mocks' |
25 | | - |
26 | | -## Usage |
27 | | - |
28 | | -### Mock |
29 | | - |
30 | | -Creates a test double representing `string_or_model_class` with common |
31 | | -ActiveModel methods stubbed out. Additional methods may be easily stubbed |
32 | | -(via `add_stubs`) if `stubs` is passed. This is most useful for impersonating |
33 | | -models that don't exist yet. |
34 | | - |
35 | | -ActiveModel methods, plus `new_record?`, are stubbed out implicitly. |
36 | | -`new_record?` returns the inverse of `persisted?`, and is present only for |
37 | | -compatibility with extension frameworks that have yet to update themselves to |
38 | | -the ActiveModel API (which declares `persisted?`, not `new_record?`). |
39 | | - |
40 | | -`string_or_model_class` can be any of: |
41 | | - |
42 | | - * A String representing a Class that does not exist |
43 | | - * A String representing a Class that extends `ActiveModel::Naming` |
44 | | - * A Class that extends `ActiveModel::Naming` |
45 | | - |
46 | | -### Stub |
47 | | - |
48 | | -Creates an instance of `Model` with `to_param` stubbed using a generated value |
49 | | -that is unique to each object. If `Model` is an `ActiveRecord` model, it is |
50 | | -prohibited from accessing the database. |
51 | | - |
52 | | -For each key in `stubs`, if the model has a matching attribute (determined by |
53 | | -`respond_to?`) it is simply assigned the submitted values. If the model does |
54 | | -not have a matching attribute, the key/value pair is assigned as a stub return |
55 | | -value using RSpec's mocking/stubbing framework. |
56 | | - |
57 | | -`persisted?` is overridden to return the result of `!id.nil?` This means that |
58 | | -by default `persisted?` will return true. If you want the object to behave as a |
59 | | -new record, sending it `as_new_record` will set the id to nil. You can also |
60 | | -explicitly set `:id => nil`, in which case `persisted?` will return false, but |
61 | | -using `as_new_record` makes the example a bit more descriptive. |
62 | | - |
63 | | -While you can use `stub_model` in any example (model, view, controller, |
64 | | -helper), it is especially useful in view examples, which are inherently more |
65 | | -state-based than interaction-based. |
66 | | - |
67 | | - stub_model(Person) |
68 | | - stub_model(Person).as_new_record |
69 | | - stub_model(Person, :to_param => 37) |
70 | | - stub_model(Person) {|person| person.first_name = "David"} |
| 3 | +This branch is deprecated, please use `main`. |
0 commit comments