如何在Rspec中创建测试以获取用户列表

如何在Rspec中创建测试以获取用户列表,rspec,tdd,Rspec,Tdd,这是RSpec创建的默认值。我在使用TDD时遇到困难。我觉得这很复杂,而且很难理解整个过程 例如,我需要通过访问index方法来测试它,它返回一个用户列表。测试此功能的方法是什么 describe "GET 'index'" do it "returns http success" do get 'index' expect(response).to be_success end end 您可以避免控制器测试,因为所有逻辑都可以使用rspec+Capybara进行验收测试

这是RSpec创建的默认值。我在使用TDD时遇到困难。我觉得这很复杂,而且很难理解整个过程

例如,我需要通过访问index方法来测试它,它返回一个用户列表。测试此功能的方法是什么

describe "GET 'index'" do it "returns http success" do get 'index' expect(response).to be_success end end
您可以避免控制器测试,因为所有逻辑都可以使用rspec+Capybara进行验收测试。我建议买一本pdf书,这是rspec学习的最佳选择

这里我给你举个例子来回答你的问题:

feature 'Visitor' do

  background do
    2.times |n| { User.new(username: "User #{n}") } #create 2 users to test list
  end

  scenario 'tries to view list of users on users index page' do
    visit users_path #try to enter to users index page
    expect(current_path).to eq users_path #check current path
    expect(page).to have_content 'User 1' #check for User 1 from 'background'
    expect(page).to have_content 'User 2' #check for User 2 from 'background'
  end

end

简单地说,我们通过“访问”方法进入索引页,希望看到一些信息。

被您问题中的措辞弄糊涂了。您在使用TDD时遇到困难-您发现它很复杂-您不了解整个过程?我想帮助编辑您的问题,使其更清楚,以便我或其他人能够更好地回答它。