Skip to content

Commit

Permalink
Add controller spec to check post :create
Browse files Browse the repository at this point in the history
  • Loading branch information
etagwerker committed Sep 10, 2024
1 parent c189deb commit a503ac3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
33 changes: 33 additions & 0 deletions spec/controllers/gemmies_controllers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "rails_helper"

RSpec.describe GemmiesController, type: :controller do
describe "create" do
context "when the gemmy params are valid" do
it "redirects to the new gemmy page" do
post :create, params: { gemmy: { name: "next_rails" } }

expect(response).to redirect_to(gemmy_path(Gemmy.find_by(name: "next_rails")))
end

it "creates a record in the database" do
expect do
post :create, params: { gemmy: { name: "next_rails" } }
end.to change(Gemmy, :count).by(1)
end

context "when the gemmy params are invalid" do
it "renders the new gemmy page" do
post :create, params: { gemmy: { name: "" } }

expect(response).to render_template(:new)
end

it "does not create a record in the database" do
expect do
post :create, params: { gemmy: { name: "" } }
end.not_to change(Gemmy, :count)
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
if ENV["COVERAGE"]
require "simplecov"
SimpleCov.start "rails"
end

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
Expand Down

0 comments on commit a503ac3

Please sign in to comment.