Reputation: 576
I have a method in my model
class Announcement < ActiveRecord::Base
def self.get_announcements
@announcements = Announcement.where("starts <= :start_date and ends >= :end_date and disabled = false",
{:start_date => "#{Date.today}", :end_date => "#{Date.today}"})
return @announcements
end
end
I am trying to write rspec for this method, as i am new to rspec cant proceed
describe ".get_announcements" do
before { @result = FactoryGirl.create(:announcement) }
it "return announcements" do
end
end
Please help
Upvotes: 0
Views: 116
Reputation: 576
Solution for my question
describe ".get_announcements" do
before { @result = FactoryGirl.create(:announcement) }
it "return announcement" do
Announcement.get_announcements.should_not be_empty
end
end
Upvotes: 1
Reputation: 47548
describe ".get_announcements" do
let!(:announcements) { [FactoryGirl.create!(:announcement)] }
it "returns announcements" do
expect(Announcement.get_announcements).to eq announcements
end
end
Note the use of let!
to immediately (not lazily) assign to announcements
.
Does the class method really need to define an instance variable? If not, it could be refactored to:
class Announcement < ActiveRecord::Base
def self.get_announcements
Announcement.where("starts <= :start_date and ends >= :end_date and disabled = false",
{:start_date => "#{Date.today}", :end_date => "#{Date.today}"})
end
end
Upvotes: 0