Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Model Attributes Pattern #38

Closed
wants to merge 1 commit into from

4 participants

@cirdes

A rspec pattern that I been using. It was a blog post
http://blog.thefrontiergroup.com.au/2012/06/my-new-favourite-rspec-pattern/

@cirdes cirdes Model Attributes Pattern
Adding a new Better Specs
be9e9e1
@andreareginato

Hi Cirdes,

first of all thanks for your pull request. I was reading what you did and In my opinion
this shouldn't be set as a guideline. It's a clever approach, but I think the user should
be free in this "factory definition" step.

I'll be waiting for comments of other people, otherwise I'll close the issue.
Thanks again.

@andreareginato

As no one discussed about this, for the moment i close it. Thanks @cirdes for your contribution, hope to see more.

@muescha

better instead of closing this issue - add it to new section like "clever ideas" or some similar naming.

i think this clever approach could help others.

maybe it can be presented along with other possible "factory definition" steps.

@gporpino

It is an awesome contribution... i will use this and i think that it can help other people.

Please acept this!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 10, 2012
  1. @cirdes

    Model Attributes Pattern

    cirdes authored
    Adding a new Better Specs
This page is out of date. Refresh to see the latest.
Showing with 48 additions and 0 deletions.
  1. +47 −0 content/index.html
  2. +1 −0  layouts/default.html
View
47 content/index.html
@@ -917,6 +917,53 @@
</article>
+
+<article>
+
+<h1><a name="pattern">Model Attributes Pattern</a></h1>
+
+<p>
+Declare and use the model_attributes object to set attributes on an object. The benefits of this pattern is that you can set specific attributes on an object based on a context without having to re-write the call to the factory.
+</p>
+
+<p class="wrong">bad</p>
+<div>
+<pre><code class="ruby"> let(:vehicle) { FactoryGirl.create(:vehicle, :vehicle_name => vehicle_name,
+ :type_of_sale => type_of_sale) }
+context 'with name' do
+ let(:vehicle_name) { "Carzilla" }
+ let(:type_of_sale) { "New" }
+end
+</code></pre>
+</div>
+
+<p class="correct">good</p>
+
+<div>
+<pre><code class="ruby">let(:vehicle) { FactoryGirl.create(:vehicle, vehicle_attributes) }
+let(:vehicle_attributes) { {} }
+subject { vehicle.name }
+context 'with name' do
+ let(:vehicle_attributes) { {name: "Carzilla"} }
+ it { should == "Carzilla" }
+end
+</code></pre>
+</div>
+
+<p>
+Learn more about the <a href="http://blog.thefrontiergroup.com.au/2012/06/my-new-favourite-rspec-pattern/">Pattern</a>.
+</p>
+
+<p>
+<a href="https://github.com/andreareginato/betterspecs/issues/38">Discuss about this guideline &rarr;</a>
+</p>
+
+<a href="https://twitter.com/share" data-text="betterspecs.org | Model Attributes Pattern" data-url="http://betterspecs.org/#pattern" class="twitter-share-button" data-related="jasoncosta" data-lang="en" data-size="medium" data-counturl="http://betterspecs.org" data-count="vertical">Tweet</a>
+<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
+<div class="fb-like" data-href="http://betterspecs.org/#pattern" data-send="false" data-layout="box_count" data-width="450" data-show-faces="true"></div>
+
+</article>
+
<article>
<h1><a name="books">Books</a></h1>
View
1  layouts/default.html
@@ -74,6 +74,7 @@
<li class="little">&raquo; <a href="#spork">Faster tests with spork</a></li>
<li class="little">&raquo; <a href="#http">Mocking HTTP requests</a></li>
<li class="little">&raquo; <a href="#formatter">Useful formatter</a></li>
+ <li class="little">&raquo; <a href="#pattern">Model Attributes Pattern</a></li>
<li><a href="#books">Books</a></li>
<li><a href="#presentations">Presentations</a></li>
<li><a href="#resources">Resources on the web</a></li>
Something went wrong with that request. Please try again.