Permalink
Browse files

Merge branch 'thomas-holmes-remove-mocks'

  • Loading branch information...
2 parents bf8b1e9 + 8e9081a commit a8eee185c394ccf1bd89ea681fdc0c3a20bfadbd @alindeman alindeman committed Mar 3, 2014
View
@@ -1,6 +1,11 @@
### 3.0.0.rc1 Development
[Full Changelog](http://github.com/rspec/rspec-rails/compare/v3.0.0.beta2...master)
+Breaking Changes for 3.0.0:
+
+* Extracts the `mock_model` and `stub_model` methods to the
+ `rspec-activemodel-mocks` gem. (Thomas Holmes)
+
### 3.0.0.beta2 / 2014-02-17
[Full Changelog](http://github.com/rspec/rspec-rails/compare/v3.0.0.beta1...v3.0.0.beta2)
@@ -36,7 +36,7 @@ Feature: render_template matcher
describe "gadgets/index" do
it "renders the index template" do
- assign(:gadgets, [stub_model(Gadget)])
+ assign(:gadgets, [Gadget.create!])
render
expect(view).to render_template(:index)
@@ -1,147 +0,0 @@
-Feature: mock_model
-
- The `mock_model` method generates a test double that acts like an instance of
- `ActiveModel`. This is different from the `stub_model` method which generates
- an instance of a real model class.
-
- The benefit of `mock_model` over `stub_model` is that it is a true double, so
- the examples are not dependent on the behavior (or mis-behavior), or even the
- existence of any other code. If you're working on a controller spec and you
- need a model that doesn't exist, you can pass `mock_model` a string and the
- generated object will act as though its an instance of the class named by
- that string.
-
- Scenario: passing a string that represents a non-existent constant
- Given a file named "spec/models/car_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe "mock_model('Car') with no Car constant in existence" do
- it "generates a constant" do
- expect(Object.const_defined?(:Car)).to be_falsey
- mock_model("Car")
- expect(Object.const_defined?(:Car)).to be_truthy
- end
-
- describe "generates an object that ..." do
- it "returns the correct name" do
- car = mock_model("Car")
- expect(car.class.name).to eq("Car")
- end
-
- it "says it is a Car" do
- car = mock_model("Car")
- expect(car).to be_a(Car)
- end
- end
- end
- """
- When I run `rspec spec/models/car_spec.rb`
- Then the examples should all pass
-
- Scenario: passing a string that represents an existing constant
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe Widget do
- it "uses the existing constant" do
- widget = mock_model("Widget")
- expect(widget).to be_a(Widget)
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
-
- Scenario: passing a class that does not extend ActiveModel::Naming
- Given a file named "spec/models/string_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe String do
- it "raises" do
- expect { mock_model(String) }.to raise_exception
- end
- end
- """
- When I run `rspec spec/models/string_spec.rb`
- Then the examples should all pass
-
- Scenario: passing an Active Record constant
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe Widget do
- let(:widget) { mock_model(Widget) }
-
- it "is valid by default" do
- expect(widget).to be_valid
- end
-
- it "is not a new record by default" do
- expect(widget).not_to be_new_record
- end
-
- it "can be converted to a new record" do
- expect(widget.as_new_record).to be_new_record
- end
-
- it "sets :id to nil upon destroy" do
- widget.destroy
- expect(widget.id).to be_nil
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
-
- Scenario: passing an Active Record constant with method stubs
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe "mock_model(Widget) with stubs" do
- let(:widget) do
- mock_model Widget, :foo => "bar",
- :save => true,
- :update_attributes => false
- end
-
- it "supports stubs for methods that don't exist in ActiveModel or ActiveRecord" do
- expect(widget.foo).to eq("bar")
- end
-
- it "supports stubs for methods that do exist" do
- expect(widget.save).to eq(true)
- expect(widget.update_attributes).to be_falsey
- end
-
- describe "#errors" do
- context "with update_attributes => false" do
- it "is not empty" do
- expect(widget.errors).not_to be_empty
- end
- end
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
-
- Scenario: mock_model outside rails
- Given a file named "mock_model_outside_rails_spec.rb" with:
- """ruby
- require 'rspec/rails/mocks'
-
- describe "Foo" do
- it "is mockable" do
- foo = mock_model("Foo")
- expect(foo.id).to eq(1001)
- expect(foo.to_param).to eq("1001")
- end
- end
- """
- When I run `rspec mock_model_outside_rails_spec.rb`
- Then the examples should all pass
@@ -1,58 +0,0 @@
-Feature: stub_model
-
- The stub_model method generates an instance of a Active Model model.
-
- While you can use stub_model in any example (model, view, controller,
- helper), it is especially useful in view examples, which are inherently
- more state-based than interaction-based.
-
- Scenario: passing an Active Record constant with a hash of stubs
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe "stub_model(Widget) with a hash of stubs" do
- let(:widget) do
- stub_model Widget, :id => 5, :random_attribute => true
- end
-
- it "stubs :id" do
- expect(widget.id).to eql(5)
- end
-
- it "stubs :random_attribute" do
- expect(widget.random_attribute).to be_truthy
- end
-
- it "returns false for new_record? if :id is set" do
- expect(widget).not_to be_new_record
- end
-
- it "can be converted to a new record" do
- widget.as_new_record
- expect(widget).to be_new_record
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
-
- Scenario: passing an Active Record constant with a block of stubs
- Given a file named "spec/models/widget_spec.rb" with:
- """ruby
- require "spec_helper"
-
- describe "stub_model(Widget) with a block of stubs" do
- let(:widget) do
- stub_model Widget do |widget|
- widget.id = 5
- end
- end
-
- it "stubs :id" do
- expect(widget.id).to eql(5)
- end
- end
- """
- When I run `rspec spec/models/widget_spec.rb`
- Then the examples should all pass
@@ -11,8 +11,8 @@ Feature: stub template
describe "gadgets/list" do
it "renders the gadget partial for each gadget" do
assign(:gadgets, [
- mock_model(Gadget, :id => 1, :name => "First"),
- mock_model(Gadget, :id => 2, :name => "Second")
+ double(:name => "First"),
+ double(:name => "Second")
])
stub_template "gadgets/_gadget.html.erb" => "<%= gadget.name %><br/>"
render
@@ -36,7 +36,7 @@ Feature: stub template
describe "gadgets/edit" do
before(:each) do
- @gadget = assign(:gadget, stub_model(Gadget))
+ @gadget = assign(:gadget, Gadget.create!)
end
it "renders the form partial" do
@@ -10,8 +10,8 @@ Feature: view spec
describe "widgets/index" do
it "displays all the widgets" do
assign(:widgets, [
- stub_model(Widget, :name => "slicer"),
- stub_model(Widget, :name => "dicer")
+ Widget.create!(:name => "slicer"),
+ Widget.create!(:name => "dicer")
])
render
@@ -34,8 +34,8 @@ Feature: view spec
context "with 2 widgets" do
before(:each) do
assign(:widgets, [
- stub_model(Widget, :name => "slicer"),
- stub_model(Widget, :name => "dicer")
+ Widget.create!(:name => "slicer"),
+ Widget.create!(:name => "dicer")
])
end
@@ -58,7 +58,7 @@ Feature: view spec
describe "rendering the widget template" do
it "displays the widget" do
- assign(:widget, stub_model(Widget, :name => "slicer"))
+ assign(:widget, Widget.create!(:name => "slicer"))
render :template => "widgets/widget.html.erb"
@@ -112,7 +112,7 @@ Feature: view spec
describe "rendering locals in a partial" do
it "displays the widget" do
- widget = stub_model(Widget, :name => "slicer")
+ widget = Widget.create!(:name => "slicer")
render :partial => "widgets/widget.html.erb", :locals => {:widget => widget}
@@ -134,7 +134,7 @@ Feature: view spec
describe "rendering locals in a partial" do
it "displays the widget" do
- widget = stub_model(Widget, :name => "slicer")
+ widget = Widget.create!(:name => "slicer")
render "widgets/widget", :widget => widget
@@ -3,7 +3,7 @@
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
describe "<%= ns_table_name %>/edit" do
before(:each) do
- @<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
+ @<%= ns_file_name %> = assign(:<%= ns_file_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
:<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
@@ -5,7 +5,7 @@
before(:each) do
assign(:<%= table_name %>, [
<% [1,2].each_with_index do |id, model_index| -%>
- stub_model(<%= class_name %><%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : ',' %>
+ <%= class_name %>.create!(<%= output_attributes.empty? ? (model_index == 1 ? ')' : '),') : '' %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
:<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
@@ -3,11 +3,11 @@
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
describe "<%= ns_table_name %>/new" do
before(:each) do
- assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? ').as_new_record)' : ',' %>
+ assign(:<%= ns_file_name %>, <%= class_name %>.new(<%= '))' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
:<%= attribute.name %> => <%= attribute.default.inspect %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
-<%= !output_attributes.empty? ? " ).as_new_record)\n end" : " end" %>
+<%= !output_attributes.empty? ? " ))\n end" : " end" %>
it "renders new <%= ns_file_name %> form" do
render
@@ -3,7 +3,7 @@
<% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
describe "<%= ns_table_name %>/show" do
before(:each) do
- @<%= ns_file_name %> = assign(:<%= ns_file_name %>, stub_model(<%= class_name %><%= output_attributes.empty? ? '))' : ',' %>
+ @<%= ns_file_name %> = assign(:<%= ns_file_name %>, <%= class_name %>.create!(<%= '))' if output_attributes.empty? %>
<% output_attributes.each_with_index do |attribute, attribute_index| -%>
:<%= attribute.name %> => <%= value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
<% end -%>
View
@@ -12,6 +12,5 @@
require 'rspec/rails/adapters'
require 'rspec/rails/matchers'
require 'rspec/rails/fixture_support'
-require 'rspec/rails/mocks'
require 'rspec/rails/example'
require 'rspec/rails/vendor/capybara'
Oops, something went wrong.

0 comments on commit a8eee18

Please sign in to comment.