My function works in practice but does not pass tests rspec

36 views Asked by At

Perhaps it is my lack of familiarity with rspec but i do not understand what is going on with my test.

I have 2 classes one called Scrape, the other Result (creative) Scrape is a web scraping class that searches a site and scrapes the results from the page, creating a new Result instance from each.

Result instances are stored in a class variable array accessible via Result.all

this works in practice in the actual program, however when I tried to write a test for this behavior it fails.

    describe "#scrape_results" do
        it "accepts a url scrapes the page and creates a Result for each" do
            s = Scrape.new
            s.scrape_results(@url)
            expect(Result.all.count).not_to eq(0)
        end
    end

every time i run the test Result.all.count is 0 if i use pry and manually run #scrape_results the test passes.

I appreciate your time, patience, and help thanks

1

There are 1 answers

0
Tim Urian On

I notice that you are passing @url to #scrape_results in your test. Unless you are defining that variable inside of the describe block or the test block it will be nil in your test. It is possible that since @url might be something other than nil from wherever you are pry-ing which is causing the Result to be created and the test to pass.