Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / doc / src / documentation / mocks / partial_mocks.page
blob2dc2cb45ac6807c6a36e15d2bd203422411e5ea9
1 ---
2 title: Partial Mocks
3 ---
4 h2. Partial Mocks
6 RSpec allows you <a href="stubs.html">stub methods</a> and set <a href="mocks.html">mock
7 expectations</a> for methods on existing Class objects. The main reason for including
8 this is to support mocking/stubbing Ruby on Rails model class methods in controller and/or view specs.
10 <ruby>
11 MyModel.should_receive(:find).with(id).and_return(@mock_model_instance)
12 </ruby>
14 <a href="mocks.html">Mocking</a> (and/or <a href="stubs.html">stubbing</a>) the class
15 level methods and having them return a mock instead of a real instance
16 of the model class allows you to spec your controllers and views in isolation from the
17 instance level logic of your model classes. This means that you can change the validation
18 rules for a model, for example, and drive that in the model specs without affecting the
19 controller and view specs.
21 This also helps to keep the context of your spec completely in view (no having to look at fixtures/xyz.yml to understand what's going on).
23 Additionally, although we haven't completely isolated specs from the
24 database yet (you still need one for the models to actually work), this sort of mocking
25 will save you trips to the database, speeding things up quite a bit in a larger app.