require "application_system_test_case" class PostsTest < ApplicationSystemTestCase test "visiting the index" do visit posts_url assert_selector "h1", text: "Posts" end end
The test visits the URL for posts index (posts_url) and asserts that an h1 element containing the text 'Posts' is present on the page.
test "creating a post" do visit new_post_url fill_in "Title", with: "My Post" click_button "Create Post" assert_text "Post was successfully created" end
In Rails system tests, test methods must be inside a test class that inherits from ApplicationSystemTestCase. The snippet is missing the class wrapper.
test "creating a post increases count" do post_count = Post.count visit new_post_url fill_in "Title", with: "New Post" click_button "Create Post" assert_text "Post was successfully created" post_count = Post.count end
The test checks the post count before and after creating a post. After clicking 'Create Post', the post is saved, so the count increases by 1.
test "deleting a post" do visit posts_url click_on "Delete", match: :first assert_text "Post was successfully destroyed" end
The error means Capybara cannot find any element with text 'Delete'. This usually happens if the link or button is missing or named differently.
Capybara's 'visit' method opens the given URL in a browser controlled by the test driver (like Selenium). This simulates a real user visiting the page.
