以下は、Request Specs と Capybara に関する Ryan Bates Railscastからの抜粋です。
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
そして、これは 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"
(ここに 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