How to make these simple tests pass in an elegant way? Ruby on Rails

14 views
Skip to first unread message

Star Track

unread,
May 29, 2014, 12:54:26 AM5/29/14
to rubyonra...@googlegroups.com
I am writing a rails app with my colleague who wrote a lot of the tests.  He had to take a leave of absence due to a death in his family, and I am needing helps with changing the model so that the tests will pass in our spec file.

Here is the model:

    class Worker < ActiveRecord::Base
      attr_accessible :first_name, :last_name
     
      has_many :writings
    end




Here is the spec:

    require 'spec_helper.rb'
   
    describe Worker do
      describe "merging two workers" do
        before(:each) do
          @writing1 = Writing.create!(:writing_text => "Winner's note")
          @writing2 = Writing.create!(:writing_text => "Loser's note")
         
          @winner = Worker.create!(:first_name => "Jim", :last_name => "Sullivan", :email => "win...@gmail.com")
          @winner.writings << @writing1
         
          @loser  = Worker.create!(:first_name => "Tom", :last_name => "Smithe"
          @loser.writings << @writing2
         
          @result = Worker.merge(@winner, @loser)
        end
       
        it "should return the winner" do
          @result.id.should eq @winner.id
        end
         
        it "should not edit the winner's name" do
          @result.first_name.should eq @winner.first_name
          @result.last_name.should eq @winner.last_name
        end
    end
    end


All of these tests are failing, and I'm not sure what to do.  I don't want to try something and accidentally break the model, as I am new to ruby on rails.  Thanks.

Fails:

Failures:

 

        1) Worker merging two worker should return the winner
         Failure/Error: @result = Worker.merge(@winner, @loser)
         NoMethodError:
           undefined method `merge' for #<Class:0x007fb612ee2ff8>
         # ./spec/models/worker_spec.rb:15:in `block (3 levels) in <top (required)>'
   
      2) Person merging two people should not edit the winner's name
         Failure/Error: @result = Worker.merge(@winner, @loser)
         NoMethodError:
           undefined method `merge' for #<Class:0x007fb612ee2ff8>
         # ./spec/models/worker_spec.rb:15:in `block (3 levels) in <top (required)>'
   
      3) Person merging two people should merge the winner's email only if he has no email
         Failure/Error: @result = Worker.merge(@winner, @loser)
         NoMethodError:
           undefined method `merge' for #<Class:0x007fb612ee2ff8>
         # ./spec/models/worker_spec.rb:15:in `block (3 levels) in <top (required)>'
   
      4) Person merging two people should not edit the winner's company name
         Failure/Error: @result = Worker.merge(@winner, @loser)
         NoMethodError:
           undefined method `merge' for #<Class:0x007fb612ee2ff8>
         # ./spec/models/worker_spec.rb:15:in `block (3 levels) in <top (required)>'
   
      5) Person merging two people should delete the loser
         Failure/Error: @result = Worker.merge(@winner, @loser)
         NoMethodError:
           undefined method `merge' for #<Class:0x007fb612ee2ff8>
         # ./spec/models/worker_spec.rb:15:in `block (3 levels) in <top (required)>'
   
      6) Person merging two people should merge the notes together
         Failure/Error: @result = Worker.merge(@winner, @loser)
         NoMethodError:
           undefined method `merge' for #<Class:0x007fb612ee2ff8>
         # ./spec/models/worker_spec.rb:15:in `block (3 levels) in <top (required)>'


Frederick Cheung

unread,
May 29, 2014, 1:29:53 PM5/29/14
to rubyonra...@googlegroups.com
On Thursday, May 29, 2014 5:54:26 AM UTC+1, Star Track wrote:
> I am writing a rails app with my colleague who wrote a lot of the tests.  He had to take a leave of absence due to a death in his family, and I am needing helps with changing the model so that the tests will pass in our spec file.
>
[snip]
> All of these tests are failing, and I'm not sure what to do.  I don't want to try something and accidentally break the model, as I am new to ruby on rails.  Thanks.
>

These specs are testing a method that doesn't exist yet ( it is pretty common to start by writing some specs and then writing the implementation). If you want them to pass you'll have to write a merge method that implements the behaviour described by the specs - hopefully the specs are descriptive enough to allow you to do this


Fred
Reply all
Reply to author
Forward
0 new messages