class Snapshot < ActiveRecord::Base
def self.grab_website(uri)
response, content = Internet.access_webpage(uri)
# code that calculates the hash, saves the content or generates an error log messages
end
end
stub_const("Internet::WEBSITE_REPLIES", \
{ "http://www.thiswebsiteisimportanttome.com" => \
[["2015-06-12 15:30", "website_Version_A.html"] ,\
["2015-06-14 18:00", "website_Version_B.html"] ,\
["2015-06-14 18:50", 404] ,\
["2015-06-14 19:00", "website_Version_C.html"] ,\
["2015-06-26 11:35", "website_Version_D.html"]] } )
allow(Internet).to receive(:access_webpage) do |uri|
time = Time.now
content = nil
response_code = 0 # We're not connected to the internet
if Internet::WEBSITE_REPLIES.has_key?(uri)
filename = nil
timetable = Internet::WEBSITE_REPLIES[uri]
timetable.each do |timepoint_situation|
timepoint = Time.parse(timepoint_situation[0])
if time > timepoint
filename = timepoint_situation[1]
end
end
if (filename.nil?)
response_code = 0
elsif (filename.kind_of? Integer)
response_code = filename
else # filename is really a filename
content = File.read(filename)
response_code = 200
end
end
next response_code, content
end
first_time = Time.local(2015, 6, 12, 15, 35)
second_time = Time.local(2015, 6, 14, 17, 00)
third_time = Time.local(2015, 6, 14, 18, 05)
uri = "http://www.thiswebsiteisimportanttome.com"
Timecop.freeze(first_time) # Version A
Snapshot.grab_website(uri)
Timecop.freeze(second_time) # Version A (no changes)
Snapshot.grab_website(uri)
Timecop.freeze(third_time) # Version B
Snapshot.grab_website(uri)
snapshots = Snapshot.all
expect(snapshots.size).to eq(2)
snapshot = snapshots[0]
expect(snapshot.time).to eq(first_time)
expect(snapshot.hash).to eq("234092384EF")
expect(snapshot.filename).to eq(...)
snapshot = snapshots[1]
expect(snapshot.time).to eq(third_time)
expect(snapshot.hash).to eq("A8E92340213")
expect(snapshot.filename).to eq(...)
# work to do: check the log messages ...
--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/c1ed74fa-8f54-40ae-a6ba-ccfab2584bc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Take a look at VCR. It is specifically designed for when you are testing HTTP-dependent code.
--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/dqkIqrX5Vgo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/CADUxQmtt_vrq%2BRTbNHC%2BE%2BRTAubATaQrz26my7Dtd8FVKx0Yqw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/CALp6bixYJXsCpgwUm7c8Dgv7bn4U--yF84yqgmDu20wWT-jJ2w%40mail.gmail.com.