1
+ require "spec_helper"
2
+ app_require "tasks/fetch_feed"
3
+
4
+ class Feed < OpenStruct ; end ;
5
+
6
+ describe FetchFeed do
7
+ let ( :daring_fireball ) {
8
+ Feed . new ( name : "Daring Fireball" , url : "http://daringfireball.net/index.xml" , last_fetched : Time . new ( 2013 , 4 , 5 ) )
9
+ }
10
+
11
+ describe "#fetch" do
12
+ it "fetches the feed" do
13
+ result = FetchFeed . new ( daring_fireball ) . fetch
14
+ result . title . should eq "Daring Fireball"
15
+ result . entries . first . author . should eq "John Gruber"
16
+ end
17
+
18
+ context "when no new posts have been added" do
19
+ it "should not add any new posts" do
20
+ fake_feed = stub ( last_modified : Time . new ( 2012 , 12 , 31 ) )
21
+ parser = stub ( fetch_and_parse : fake_feed )
22
+
23
+ StoryRepository . should_not_receive ( :add )
24
+
25
+ FetchFeed . new ( daring_fireball , parser ) . fetch
26
+ end
27
+ end
28
+
29
+ context "when new posts have been added" do
30
+ let ( :now ) { Time . now }
31
+ let ( :new_story ) { stub ( published : now + 1 ) }
32
+ let ( :old_story ) { stub ( published : Time . new ( 2009 , 4 , 20 ) ) }
33
+
34
+ let ( :fake_feed ) { stub ( last_modified : now , entries : [ new_story , old_story ] ) }
35
+ let ( :fake_parser ) { stub ( fetch_and_parse : fake_feed ) }
36
+
37
+ it "should only add posts that are new" do
38
+ StoryRepository . should_receive ( :add ) . with ( new_story , daring_fireball )
39
+ StoryRepository . should_not_receive ( :add ) . with ( old_story , daring_fireball )
40
+
41
+ FetchFeed . new ( daring_fireball , fake_parser ) . fetch
42
+ end
43
+
44
+ it "should update the last fetched time for the feed" do
45
+ FeedRepository . should_receive ( :update_last_fetched )
46
+ . with ( daring_fireball , now )
47
+
48
+ FetchFeed . new ( daring_fireball , fake_parser ) . fetch
49
+ end
50
+ end
51
+ end
52
+ end
0 commit comments