{"id":692,"date":"2022-08-30T15:05:35","date_gmt":"2022-08-30T15:05:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/rspec2-throws-undefined-method-for-model-class-method-self-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:35","modified_gmt":"2022-08-30T15:05:35","slug":"rspec2-throws-undefined-method-for-model-class-method-self-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/rspec2-throws-undefined-method-for-model-class-method-self-collection-of-common-programming-errors\/","title":{"rendered":"rspec2 throws &#39;undefined method&#39; for model class method (.self)-Collection of common programming errors"},"content":{"rendered":"<p>I have a model (event.rb) method, that retrieves a list of all recurrence dates for the named period<\/p>\n<pre><code>  def self.dates_between(start_date, end_date)\n     dates = (start_date..end_date).step(7).to_a\n  end\n<\/code><\/pre>\n<p>than I specify the following in event_spec.rb<\/p>\n<pre><code>  before(:each) do\n    @event = FactoryGirl.create(:event)\n  end    \n  subject { @event }\n\n  ... other working tests ...\n\n  describe '#dates_between' do\n    context 'finds recurrences dates of a event' do\n      start_date = \"2012-12-01 18:25:25\"\n      end_date = \"2012-12-15 18:25:25\"\n      output_dates = [\"2012-12-01 18:25:25\", \"2012-12-08 18:25:25\", \"2012-12-15 18:25:25\"]\n\n      it 'should call Event with method dates_between' do\n        @event.should_receive(:dates_between).with(start_date, end_date)\n        @event.dates_between(start_date, end_date)\n      end\n\n      it 'should find and return the RIGHT recurrences dates' do\n        @event.dates_between(start_date, end_date).should eq(output_dates)\n      end\n    end\n  end\n<\/code><\/pre>\n<p>and get this failure:<\/p>\n<pre><code>1) Event#dates_between finds recurrences dates of a event should find and return the RIGHT recurrences dates\n Failure\/Error: @event.dates_between(start_date, end_date).should eq(output_dates)\n NoMethodError:\n   undefined method `dates_between' for #\n # .\/spec\/models\/event_spec.rb:52:in `block (4 levels) in '\n<\/code><\/pre>\n<p>when I change in the model from a class method to an instance method (removing &#8220;self.&#8221;) the console just prints out &#8220;wild data&#8221;:<\/p>\n<blockquote>\n<p>22:93:55&#8243;, &#8220;2012-12-01 22:93:62&#8221;, &#8220;2012-12-01 22:93:69&#8221;, &#8220;2012-12-01 22:93:76&#8221;, &#8220;2012-12-01 22:93:83&#8221;, &#8220;2012-12-01 22:93:90&#8221;, &#8220;2012-12-01 22:93:97&#8221;, &#8220;2012-12-01 22:94:04&#8221;, &#8220;2012-12-01 22:94:11&#8221;, &#8220;2012-12-01 22:94:18&#8221;, &#8220;2012-12-01 22:94:25&#8221;, &#8220;2012-12-01 22:94:32&#8221;, &#8230;<\/p>\n<\/blockquote>\n<p>any ideas?<\/p>\n<ol>\n<li>\n<p>So, I got it working, I made two faults:<\/p>\n<ul>\n<li>\n<p>First, I needed to call on a class method (Event.dates_between) and not on an instance method (Event.new.dates_between)<\/p>\n<\/li>\n<li>\n<p>Second, I expected<\/p>\n<p>[&#8220;2012-12-01 18:25:25&#8221;, &#8220;2012-12-08 18:25:25&#8221;, &#8220;2012-12-15 18:25:25&#8221;]<\/p>\n<\/li>\n<\/ul>\n<p>but should have expexted without time, which screwed up my console by iterating through every second &#8211; minute &#8211; hour for the three expected days<\/p>\n<pre><code>[\"2012-12-01\", \"2012-12-08\", \"2012-12-15\"] \n<\/code><\/pre>\n<p>No I do following and the specs are green:<\/p>\n<pre><code>describe Event do\n\n  subject(:event) { FactoryGirl.create(:event) }\n\n  describe '#dates_between' do\n    context 'finds recurrences dates of a event' do\n      start_date = \"2012-12-01\"\n      end_date = \"2012-12-15\"\n      output_dates = [\"2012-12-01\", \"2012-12-08\", \"2012-12-15\"]\n\n      it 'should call dates_between with two arguments' do\n        event.should_receive(:dates_between).with(start_date, end_date).and_return(output_dates)\n        event.dates_between(start_date, end_date).should eq(output_dates)\n      end\n\n      it 'should find and return the RIGHT recurrences dates' do\n        Event.dates_between(start_date, end_date).should eq(output_dates)\n      end\n    end\n  end\n\nend\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:39:49. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a model (event.rb) method, that retrieves a list of all recurrence dates for the named period def self.dates_between(start_date, end_date) dates = (start_date..end_date).step(7).to_a end than I specify the following in event_spec.rb before(:each) do @event = FactoryGirl.create(:event) end subject { @event } &#8230; other working tests &#8230; describe &#8216;#dates_between&#8217; do context &#8216;finds recurrences dates of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-692","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=692"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/692\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}