diff --git a/Gemfile b/Gemfile index faee38102..8fe09c6c2 100644 --- a/Gemfile +++ b/Gemfile @@ -80,4 +80,5 @@ group :development, :test do gem 'webrat' gem 'watchr' gem 'spork', '~> 0.9.0.rc' + gem 'factory_girl_rails', '~> 4.0' end diff --git a/Gemfile.lock b/Gemfile.lock index 70f01a3d1..0284230df 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,11 @@ GEM eventmachine (1.0.0) execjs (1.4.0) multi_json (~> 1.0) + factory_girl (4.1.0) + activesupport (>= 3.0.0) + factory_girl_rails (4.1.0) + factory_girl (~> 4.1.0) + railties (>= 3.0.0) fastthread (1.0.7) friendly_id (4.0.9) fssm (0.2.9) @@ -204,6 +209,7 @@ DEPENDENCIES compass-rails (~> 1.0.3) devise diff-lcs + factory_girl_rails (~> 4.0) friendly_id gravatar-ultimate haml diff --git a/spec/factories/crop.rb b/spec/factories/crop.rb new file mode 100644 index 000000000..88eb6af2d --- /dev/null +++ b/spec/factories/crop.rb @@ -0,0 +1,17 @@ +FactoryGirl.define do + + factory :crop do + + factory :tomato do + system_name "Tomato" + en_wikipedia_url "http://en.wikipedia.org/wiki/Tomato" + end + + factory :maize do + system_name "Maize" + en_wikipedia_url "http://en.wikipedia.org/wiki/Maize" + end + + end + +end diff --git a/spec/factories/scientific_name.rb b/spec/factories/scientific_name.rb new file mode 100644 index 000000000..7bb3e4cd9 --- /dev/null +++ b/spec/factories/scientific_name.rb @@ -0,0 +1,12 @@ +FactoryGirl.define do + factory :scientific_name do + factory :zea_mays do + association :crop, factory: :maize + scientific_name "Zea mays" + end + factory :solanum_lycopersicum do + association :crop, factory: :tomato + scientific_name "Solanum lycopersicum" + end + end +end diff --git a/spec/factories/user.rb b/spec/factories/user.rb new file mode 100644 index 000000000..d5d3dc5cb --- /dev/null +++ b/spec/factories/user.rb @@ -0,0 +1,15 @@ +FactoryGirl.define do + + factory :user do + username "user1" + password "password1" + email "user1@example.com" + tos_agreement true + + factory :confirmed_user do + confirmed_at Time.now() + end + + end + +end diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index b02726ca4..36f645451 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -4,9 +4,7 @@ describe Crop do context 'all fields present' do before(:each) do - @crop = Crop.new - @crop.system_name = "Tomato" - @crop.en_wikipedia_url = "http://en.wikipedia.org/wiki/Tomato" + @crop = FactoryGirl.create(:tomato) end it 'should save a basic crop' do @@ -36,10 +34,7 @@ describe Crop do context 'random' do before(:each) do - @crop = Crop.new - @crop.system_name = "Tomato" - @crop.en_wikipedia_url = "http://en.wikipedia.org/wiki/Tomato" - @crop.save + @crop = FactoryGirl.create(:tomato) end it 'should find a random crop' do diff --git a/spec/models/scientific_name_spec.rb b/spec/models/scientific_name_spec.rb new file mode 100644 index 000000000..7cfe62ea1 --- /dev/null +++ b/spec/models/scientific_name_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe ScientificName do + context 'all fields present' do + + before(:each) do + @sn = FactoryGirl.create(:zea_mays) + end + + it 'should save a basic scientific name' do + @sn.save.should be_true + end + + it 'should be fetchable from the database' do + @sn.save + @sn2 = ScientificName.find_by_scientific_name('Zea mays') + @sn2.crop.system_name.should == "Maize" + end + end + + context 'invalid data' do + it 'should not save a scientific name without a name' do + @sn = ScientificName.new + expect { @sn.save }.to raise_error ActiveRecord::StatementInvalid + end + end +end diff --git a/spec/views/crops/edit.html.haml_spec.rb b/spec/views/crops/edit.html.haml_spec.rb index 51f442e4e..c400c83a0 100644 --- a/spec/views/crops/edit.html.haml_spec.rb +++ b/spec/views/crops/edit.html.haml_spec.rb @@ -2,10 +2,7 @@ require 'spec_helper' describe "crops/edit" do before(:each) do - @crop = assign(:crop, stub_model(Crop, - :system_name => "MyString", - :en_wikipedia_url => "MyString" - )) + @crop = assign(:crop, FactoryGirl.create(:maize)) end context "logged out" do @@ -17,14 +14,12 @@ describe "crops/edit" do context "logged in" do before(:each) do - @user = User.create(:email => "growstuff@example.com", :password => "irrelevant") - @user.confirm! + @user = FactoryGirl.create(:confirmed_user) sign_in @user render end it "renders the edit crop form" do - render # Run the generator again with the --webrat flag if you want to use webrat matchers assert_select "form", :action => crops_path(@crop), :method => "post" do assert_select "input#crop_system_name", :name => "crop[system_name]" diff --git a/spec/views/crops/index.html.haml_spec.rb b/spec/views/crops/index.html.haml_spec.rb index 6360bf20d..861806dec 100644 --- a/spec/views/crops/index.html.haml_spec.rb +++ b/spec/views/crops/index.html.haml_spec.rb @@ -2,16 +2,10 @@ require 'spec_helper' describe "crops/index" do before(:each) do - assign(:crops, [ - stub_model(Crop, - :system_name => "Maize", - :en_wikipedia_url => "http://en.wikipedia.org/wiki/Maize" - ), - stub_model(Crop, - :system_name => "Tomato", - :en_wikipedia_url => "http://en.wikipedia.org/wiki/Tomato" - ) - ]) + assign(:crops, [ + FactoryGirl.create(:tomato), + FactoryGirl.create(:maize) + ]) end it "renders a list of crops" do @@ -20,12 +14,12 @@ describe "crops/index" do assert_select "a", :text => "Maize" assert_select "a", :text => "Tomato" end - + it "counts the number of crops" do render rendered.should contain "Displaying 2 crops" end - + context "logged out" do it "doesn't show the new crop link if logged out" do render diff --git a/spec/views/crops/new.html.haml_spec.rb b/spec/views/crops/new.html.haml_spec.rb index 0565ef03b..d92ef4fb0 100644 --- a/spec/views/crops/new.html.haml_spec.rb +++ b/spec/views/crops/new.html.haml_spec.rb @@ -2,10 +2,7 @@ require 'spec_helper' describe "crops/new" do before(:each) do - assign(:crop, stub_model(Crop, - :system_name => "MyString", - :en_wikipedia_url => "MyString" - ).as_new_record) + assign(:crop, FactoryGirl.create(:maize)) end context "logged out" do @@ -18,8 +15,7 @@ describe "crops/new" do context "logged in" do before(:each) do - @user = User.create(:email => "growstuff@example.com", :password => "irrelevant") - @user.confirm! + @user = FactoryGirl.create(:confirmed_user) sign_in @user render end diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index 384b87fdb..2e165c8ab 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -2,15 +2,9 @@ require 'spec_helper' describe "crops/show" do before(:each) do - @crop = assign(:crop, stub_model(Crop, - :id => 1, - :system_name => "Corn", - :en_wikipedia_url => "http://en.wikipedia.org/Maize" + @crop = assign(:crop, FactoryGirl.create(:maize, + :scientific_names => [ FactoryGirl.create(:zea_mays) ] )) - @crop.scientific_names.create( - :scientific_name => "Zea mays", - :crop_id => 1 - ) end it "shows the wikipedia URL" do @@ -31,7 +25,7 @@ describe "crops/show" do it "links to the right crop in the planting link" do render - assert_select("a[href=#{new_planting_path}?crop_id=1]") + assert_select("a[href=#{new_planting_path}?crop_id=#{@crop.id}]") end context "logged out" do diff --git a/spec/views/scientific_names/edit.html.haml_spec.rb b/spec/views/scientific_names/edit.html.haml_spec.rb index e0ebb532b..938f88c09 100644 --- a/spec/views/scientific_names/edit.html.haml_spec.rb +++ b/spec/views/scientific_names/edit.html.haml_spec.rb @@ -2,10 +2,9 @@ require 'spec_helper' describe "scientific_names/edit" do before(:each) do - @scientific_name = assign(:scientific_name, stub_model(ScientificName, - :scientific_name => "MyString", - :crop_id => 1 - )) + @scientific_name = assign(:scientific_name, + FactoryGirl.create(:zea_mays) + ) end context "logged out" do @@ -17,8 +16,7 @@ describe "scientific_names/edit" do context "logged in" do before(:each) do - @user = User.create(:email => "growstuff@example.com", :password => "irrelevant") - @user.confirm! + @user = FactoryGirl.create(:confirmed_user) sign_in @user render end diff --git a/spec/views/scientific_names/index.html.haml_spec.rb b/spec/views/scientific_names/index.html.haml_spec.rb index 5708d6506..6866b5579 100644 --- a/spec/views/scientific_names/index.html.haml_spec.rb +++ b/spec/views/scientific_names/index.html.haml_spec.rb @@ -3,21 +3,15 @@ require 'spec_helper' describe "scientific_names/index" do before(:each) do assign(:scientific_names, [ - stub_model(ScientificName, - :scientific_name => "Scientific Name", - :crop_id => 1 - ), - stub_model(ScientificName, - :scientific_name => "Scientific Name", - :crop_id => 1 - ) + FactoryGirl.create(:zea_mays), + FactoryGirl.create(:solanum_lycopersicum) ]) end it "renders a list of scientific_names" do render # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "tr>td", :text => "Scientific Name".to_s, :count => 2 - assert_select "tr>td", :text => 1.to_s, :count => 2 + assert_select "tr>td", :text => "Zea mays".to_s + assert_select "tr>td", :text => "Solanum lycopersicum".to_s end end diff --git a/spec/views/scientific_names/new.html.haml_spec.rb b/spec/views/scientific_names/new.html.haml_spec.rb index 4a0822b03..8d79f0d0c 100644 --- a/spec/views/scientific_names/new.html.haml_spec.rb +++ b/spec/views/scientific_names/new.html.haml_spec.rb @@ -2,10 +2,7 @@ require 'spec_helper' describe "scientific_names/new" do before(:each) do - assign(:scientific_name, stub_model(ScientificName, - :scientific_name => "MyString", - :crop_id => 1 - ).as_new_record) + assign(:scientific_name, FactoryGirl.create(:zea_mays)) end context "logged out" do @@ -17,8 +14,7 @@ describe "scientific_names/new" do context "logged in" do before(:each) do - @user = User.create(:email => "growstuff@example.com", :password => "irrelevant") - @user.confirm! + @user = FactoryGirl.create(:confirmed_user) sign_in @user render end diff --git a/spec/views/scientific_names/show.html.haml_spec.rb b/spec/views/scientific_names/show.html.haml_spec.rb index 7f2a38af4..5d086e978 100644 --- a/spec/views/scientific_names/show.html.haml_spec.rb +++ b/spec/views/scientific_names/show.html.haml_spec.rb @@ -2,16 +2,15 @@ require 'spec_helper' describe "scientific_names/show" do before(:each) do - @scientific_name = assign(:scientific_name, stub_model(ScientificName, - :scientific_name => "Scientific Name", - :crop_id => 1 - )) + @scientific_name = assign(:scientific_name, + FactoryGirl.create(:zea_mays) + ) end it "renders attributes in
" do render # Run the generator again with the --webrat flag if you want to use webrat matchers - rendered.should match(/Scientific Name/) - rendered.should match(/1/) + rendered.should match(/Zea mays/) + rendered.should match(@scientific_name.id.to_s) end end