4

I am having difficulty to write specs for my program as I need to validate the facebook ID after I filled in all the correct format information. My facebook ID is retrieved from JSON response so I not sure how do I portray this in my specs. I am very new to this field and I had been stuck at this point for a long time. I will be grateful if there are some sample codes to help me in this. Below are my half-done specs.

Scenario: Use user given Fbid to validate facebook page by using "http://graph.facebook.com" (JSON Response) to match if username and fbid are the same.

My github url : https://github.com/amycheong/company_list

Updated: I had used WebMock but end up getting:

 1) Companies new company create with valid information correct facebook id should validate fbid
 Failure/Error: Company.validate_fbid('pepsi')['username'].should == "pepsi"
 WebMock::NetConnectNotAllowedError:
   Real HTTP connections are disabled. Unregistered request: GET http://graph.facebook.com:443/pepsi with headers {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}

   You can stub this request with the following snippet:

   stub_request(:get, "http://graph.facebook.com:443/pepsi").
     with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
     to_return(:status => 200, :body => "", :headers => {})

   registered request stubs:

   stub_request(:get, "https://graph.facebook.com/pepsi")

   ============================================================

I had used WebMock.disable_net_connect!(:allow => "https://graph.facebook.com/") but problem still continue.

MODEL file:

def self.validate_fbid(fbid, format = :json)
    uri = URI.parse("https://graph.facebook.com/#{fbid}")
    data = Net::HTTP.get(uri)
    return JSON.parse(data['username'])          
end

SPEC file:

before(:each) do
        stub_request(:get, "https://graph.facebook.com/pepsi").to_return(:body => { 'username' => 'pepsi'})
    end 

    describe "correct facebook id" do               
        #validate fbid
        it "should validate fbid" do            

            Company.validate_fbid('pepsi')['username'].should == "pepsi"
            WebMock.should have_requested(:get, "https://graph.facebook.com/pepsi")
        end

    end

CONTROLLER :

def create 
@company = Company.new(params[:company])

uri = URI("http://graph.facebook.com/" + @company.fbid)
data = Net::HTTP.get(uri)
username = JSON.parse(data)['username']
if !username.nil? 
    if @company.name.downcase == username.downcase
        if @company.save 
            @message = "New company created"
            redirect_to root_path
        else 
            @message = "Company create attempt failed. Please try again."
            render 'new' 
        end 
    else 
        @message = "Invalid facebook id"
        render 'new' 
    end
else 
    @message = "No such facebook id"
    render 'new'            
end             

end

shoujo_sm
  • 3,173
  • 4
  • 37
  • 60
  • 1
    `if !username.nil? ` => `if username` – apneadiving Jul 20 '13 at 09:01
  • 2
    you should use something like webmock to mock the http call – apneadiving Jul 20 '13 at 09:02
  • 1
    Or VCR: https://github.com/vcr/vcr – Nobita Jul 20 '13 at 09:53
  • Can you describe in greater detail what exactly you want to test? – Rebitzele Jul 23 '13 at 13:07
  • @Rebitzele I want to write a spec test to test the given facebook id (fbid) matches the facebook page by retrieving JSON "http://graph.facebook.com/"+fbid Some codes: http://pastebin.com/z3UTm8LK – shoujo_sm Jul 24 '13 at 03:36
  • Is your intention that the request to facebook should be made in real life? If yes, then using `WebMock.disable_net_connect!(:allow => "https://graph.facebook.com/")` is the correct approach. The only problem here is that you wrote `https`, instead of `http`, which is the beginning of the correct url. – Kristiina Jul 25 '13 at 15:55
  • @Kristiina thanks for your reply. I had changed the codes ("https/http") but I still have problem with Webmock. I am completely out of options. This is my github address: https://github.com/amycheong/company_list – shoujo_sm Jul 26 '13 at 17:18

1 Answers1

2

You are stubbing HTTPS request but doing an HTTP with port 443, That's why Webmock doesn't find the right stub. read Using Net::HTTP.get for an https url

Community
  • 1
  • 1
barvaz
  • 641
  • 5
  • 7