require 'spec_helper' describe 'LabController' do before { WebMock.allow_net_connect! } describe "showing lab list and map" do before do @zip_code = "90210" stub(Medivo::Lab).findLabs(@zip_code) { [BeverlyHillsLab1, BeverlyHillsLab2] } end it "/labs/data returns lab and zip data as json" do visit Medivo::Engine.routes.url_helpers.labs_path :zip_code => @zip_code # can't seem to get json back .. just html .. but it works in rails server just fine # so testing for the string will have to do for now page.body.should match /#{BeverlyHillsLab1.name}/i page.body.should match /#{BeverlyHillsLab2.name}/i end it "/labs/search shows search results on the map and list", :js=>true, :driver=>:selenium_chrome do visit search_labs_path zip_code: @zip_code page.body.should match /465 N ROXBURY DR STE 715, BEVERLY HILLS, CA/i page.body.should match /8737 BEVERLY BLVD STE 401, LOS ANGELES, CA/i end end describe "finding appointments" do before do @lab_code = "20060" @date = "11/01/2011" @am_pm = nil end let(:path) { Medivo::Engine.routes.url_helpers.find_appointment_labs_path lab_code: @lab_code, date: @date } it "valid request returns appointment times as json" do body = {'time'=> ["11/03/2011|08:30 AM", "11/04/2011|08:30 AM"]} mock(Medivo::Appointment).find(@lab_code, @date, @am_pm) { body } visit path page.body.should match "08:30 AM" end it "invalid request raising ( RestClient::RequestTimeout ) returns error and message info as json" do exception = RestClient.const_get("RequestTimeout") mock(Medivo::Appointment).find(@lab_code, @date, @am_pm) { raise exception } visit path page.body.should match "Could be lab_code is wrong, or labcorp server is down" end it "invalid request raising ( RestClient::Exception ) returns error and message info as json" do response = OpenStruct.new(code: 404, body: 'hi rob') exception = RestClient.const_get("Exception").new(response) mock(Medivo::Appointment).find(@lab_code, @date, @am_pm) { raise exception } visit path page.body.should match response.body end ## TODO, get this working it "invalid request raising ( Exception ) returns error and message info as json" # do #response = OpenStruct.new(code: 500 , body: 'whacky exception' ) #p exception = Exception.new(response) #mock(Medivo::Appointment).find(@lab_code, @date, @am_pm) { raise exception } #visit path #page.body.should match response.body #end end describe "making appointments" do before do @lab_code = "20060" @time = Time.parse("2011-11-01 8:30 AM UTC") @user = {first_name: 'dude', last_name: 'man', date_of_birth: '12/28/1980', email_address: 'dudeman@dude.com', phone_number: '4155551212'} @params = { lab_code: @lab_code, time: @time, user: @user } end let(:path) { Medivo::Engine.routes.url_helpers.make_appointment_labs_path } it "make appointment" do response_body = { confirmation: '123' } stub(Medivo::Appointment).make(@lab_code, @time, hash_including(@user) ) { response_body } post path, @params response.body.should == response_body.to_json end end end