Ruby on rails 如何在Rspec中为测试对象提供唯一id? context“可预购菜肴”do 让我来!(:菜肴){创建:菜肴,厨师:厨师} 让我来!(:dish2){创建:菜肴,厨师:chef2} 它“应该显示”你做什么 时区='亚洲/吉隆坡' t=本地时区(2016,7,25,20,30,0) 时间通行证(t) 访问“/” 设定交货时间 选择\u配送\u区域\u由\u厨师烹饪。厨师 访问“/预订” 期望(第页)有内容(“预订购菜单”) 在('div#displays container')内 期望(page.)有\uXPath(“//img[@src=\”{dish.image.small}\”])) 期望(第页)有_内容(菜.说明) 期望(第页)有_内容(dish.name) 期望(第页)有_内容(菜品价格) 期望(菜)。有_内容(“添加到购物车”) 期望(dish2)。有_内容(“预订单”) expect(当前路径)。to eq(预订单路径) 结束 结束 1) 参观者可以看到预先订购的菜可以添加到购物车中,准备时间超过1小时应显示菜 失败/错误:预期(菜)。有_内容(“添加到购物车”) 应在“Dock Bogisch”中找到文本“添加到购物车” #./spec/features/visitor/preorders/list_of_preorders_spec.rb:129:in'block(4层)in' #./spec/features/visitor/preorders/list_of_preorders_spec.rb:124:in'block(3层)in'

Ruby on rails 如何在Rspec中为测试对象提供唯一id? context“可预购菜肴”do 让我来!(:菜肴){创建:菜肴,厨师:厨师} 让我来!(:dish2){创建:菜肴,厨师:chef2} 它“应该显示”你做什么 时区='亚洲/吉隆坡' t=本地时区(2016,7,25,20,30,0) 时间通行证(t) 访问“/” 设定交货时间 选择\u配送\u区域\u由\u厨师烹饪。厨师 访问“/预订” 期望(第页)有内容(“预订购菜单”) 在('div#displays container')内 期望(page.)有\uXPath(“//img[@src=\”{dish.image.small}\”])) 期望(第页)有_内容(菜.说明) 期望(第页)有_内容(dish.name) 期望(第页)有_内容(菜品价格) 期望(菜)。有_内容(“添加到购物车”) 期望(dish2)。有_内容(“预订单”) expect(当前路径)。to eq(预订单路径) 结束 结束 1) 参观者可以看到预先订购的菜可以添加到购物车中,准备时间超过1小时应显示菜 失败/错误:预期(菜)。有_内容(“添加到购物车”) 应在“Dock Bogisch”中找到文本“添加到购物车” #./spec/features/visitor/preorders/list_of_preorders_spec.rb:129:in'block(4层)in' #./spec/features/visitor/preorders/list_of_preorders_spec.rb:124:in'block(3层)in',ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我正试图让rspec检查是否有“添加到购物车”的内容,以及第2道菜是否有“预购”的内容。我怎样才能让Rspec检查它?有没有可能给他们两个唯一的id并在这里调用 在视图中,为两个按钮分配ID。比如: context 'with dish available to pre-order' do let!(:dish) { create :dish, chef: chef } let!(:dish2) { create :dish, chef: chef2} it 'should show dish

我正试图让rspec检查是否有“添加到购物车”的内容,以及第2道菜是否有“预购”的内容。我怎样才能让Rspec检查它?有没有可能给他们两个唯一的id并在这里调用

在视图中,为两个按钮分配ID。比如:

context 'with dish available to pre-order' do
let!(:dish) { create :dish, chef: chef }
let!(:dish2) { create :dish, chef: chef2}

it 'should show dish' do
  Time.zone = 'Asia/Kuala_Lumpur'
  t = Time.zone.local(2016, 7, 25, 20, 30, 0)
  Timecop.travel(t)
  visit '/'
  set_delivery_time
  select_delivery_area_by_chef dish.chef
  visit '/preorders'
  expect(page).to have_content('PREORDER MENU')

  within('div#dishes-container') do
    expect(page).to have_xpath("//img[@src=\"#{dish.image.small}\"]")
    expect(page).to have_content(dish.description)
    expect(page).to have_content(dish.name)
    expect(page).to have_content(dish.price)
    expect(dish).to have_content("Add to Cart")
    expect(dish2).to have_content("Pre-Order")
    expect(current_path).to eq(preorders_path)
  end
end

1) Visitor can see preorders with dish available to add to cart, preparation time over 1 hour should show dish
 Failure/Error: expect(dish).to have_content("Add to Cart")
   expected to find text "Add to Cart" in "Dock Bogisich"
 # ./spec/features/visitor/preorders/list_of_preorders_spec.rb:129:in `block (4 levels) in <top (required)>'
 # ./spec/features/visitor/preorders/list_of_preorders_spec.rb:124:in `block (3 levels) in <top (required)>'
预购也一样

<button id="add_to_cart-{dish.id}"> ADD to Cart </button>
<button id="pre-order-{dish2.id}"> Pre-Order </button>
add-cart-btn = "button#add_to_cart-#{dish.id}"
with_in add-cart-btn do
  page.should have_content 'Add to Cart'  
end