How to test RESTful call to database without seeding data?

99 Views Asked by At

I have a dropdown box that is populated using data from the database (via Angular).

I am trying to test it with Capybara and Poltergeist using:

        select('San Francisco', from: "user_region")

As the test database starts with a clean DB, the San Francisco option in the database cannot be loaded. How should I populate the dropdown box if I have no DB?

One idea is to run the db:seed from seed.rb but I'd rather rely on less assumptions when I run my tests. Is there a way to use something like Factory Girl?

Thanks,

1

There are 1 best solutions below

0
usha On

For Example, when you have the following capybara test case, use the background block to setup the data you need for your testing

feature "Signing in" do   
  background do
    Option.create(:name => "San Francisco", :value => "something") #you could also use FactoryGirl.create(:option)   
  end

  scenario "Signing in with correct credentials" do
    visit '/sessions/new'
    fill_in 'Login', :with => '[email protected]'
    fill_in 'Password', :with => 'caplin'
    click_link 'Sign in'
    expect(page).to have_content 'Success'
  end 
end

Here is the link for factory_girl if you don't know about it already. It comes very handy in setting up data for all kinds of tests.