WebMock: Rspec - Test Facebook Validation by using

2019-07-07 08:57发布

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

1条回答
Ridiculous、
2楼-- · 2019-07-07 09:22

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

查看更多
登录 后发表回答