简体   繁体   中英

Using a rails model in Capybara tests

I set up a project where all I have to do is display a set of records from an SQLlite3 database. I have a table in that database, and I want the root page to display all the records in the database. I have this working, but now I want to set up a Capybara test to make sure that the page has the first and last record from the table on the page.

require 'rails_helper'
describe "seeing record from scotlands model " do
  specify "I can see a list of all charities" do
    visit "/"
    expect(page).to have_content "@table.first_record"
  end
end

However, the above provides no link to the model so I cannot access it. How do I get a link to the table from the test file?

Do you generally try to access real data from tests? I've always learned to keep those things separate.

I like to work with Rspec and Capybara. Here is something simple and straight forward that should accomplish what you've discussed:

require 'rails_helper'

feature "user sees all scotlands records" do

  scenario "successfully" do

    charity1 = Charity.create(name: name1, info: info1)
    charity2 = Charity.create(name: name2, info: info2)
    charity3 = Charity.create(name: name3, info: info3)

    visit root_path

    expect(page).to have_content(charity1.name)
    expect(page).to have_content(charity1.info)

    expect(page).to have_content(charity2.name)
    expect(page).to have_content(charity2.info)

    expect(page).to have_content(charity3.name)
    expect(page).to have_content(charity3.info)

  end

end

I actually usually work with FactoryGirl also. In this case it'd make things easier because you could use create_list and make as many records as you'd like with just one line of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM