这是来自Ryan Bates Railscast on Request Specs and Capybara的摘录
require 'spec_helper'
describe "Tasks" do
describe "GET /tasks" do
it "displays tasks" do
Task.create!(:name => "paint fence")
visit tasks_path
page.should have_content("paint fence")
end
end
describe "POST /tasks" do
it "creates a task" do
visit tasks_path
fill_in "Name", :with => "mow lawn"
click_button "Add"
page.should have_content("Successfully added task.")
page.should have_content("mow lawn")
end
end
end
这是the docs on RSPec Expectations的摘录
describe Counter, "#increment" do
it "should increment the count" do
expect{Counter.increment}.to change{Counter.count}.from(0).to(1)
end
# deliberate failure
it "should increment the count by 2" do
expect{Counter.increment}.to change{Counter.count}.by(2)
end
end
所以基本上,
expect { click_button "Create my account" }.not_to change(User, :count)
是 RSpec 的一部分:
expect {...}.not_to change(User, :count)
和水豚的一部分
click_button "Create my account"
(这里是a link to the Capyabara DSL——你可以搜索click_button)
听起来您正在寻找他们两者的整体示例。这不是一个完美的例子,但它可能看起来像这样:
describe "Tasks" do
describe "GET /tasks" do
it "displays tasks" do
expect { click_button "Create my account" }.not_to change(User, :count)
end
end
end