Created
April 9, 2018 14:18
-
-
Save eliasjpr/87d615101c433fdf073d6cb14d2b646f to your computer and use it in GitHub Desktop.
Amber Framework - Testing controllers example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "./spec_helper" | |
def blog_hash | |
{"title" => "Fake", "article" => "Fake"} | |
end | |
def blog_params | |
params = [] of String | |
params << "title=#{blog_hash["title"]}" | |
params << "article=#{blog_hash["article"]}" | |
params.join("&") | |
end | |
def create_blog | |
model = Blog.new(blog_hash) | |
model.save | |
model | |
end | |
class BlogControllerTest < GarnetSpec::Controller::Test | |
getter handler : Amber::Pipe::Pipeline | |
def initialize | |
@handler = Amber::Pipe::Pipeline.new | |
@handler.build :web do | |
plug Amber::Pipe::Error.new | |
plug Amber::Pipe::Session.new | |
plug Amber::Pipe::Flash.new | |
end | |
@handler.prepare_pipelines | |
end | |
end | |
describe BlogControllerTest do | |
subject = BlogControllerTest.new | |
it "renders blog index template" do | |
Blog.clear | |
response = subject.get "/blogs" | |
response.status_code.should eq(200) | |
response.body.should contain("Blogs") | |
end | |
it "renders blog show template" do | |
Blog.clear | |
model = create_blog | |
location = "/blogs/#{model.id}" | |
response = subject.get location | |
response.status_code.should eq(200) | |
response.body.should contain("Show Blog") | |
end | |
it "renders blog new template" do | |
Blog.clear | |
location = "/blogs/new" | |
response = subject.get location | |
response.status_code.should eq(200) | |
response.body.should contain("New Blog") | |
end | |
it "renders blog edit template" do | |
Blog.clear | |
model = create_blog | |
location = "/blogs/#{model.id}/edit" | |
response = subject.get location | |
response.status_code.should eq(200) | |
response.body.should contain("Edit Blog") | |
end | |
it "creates a blog" do | |
Blog.clear | |
response = subject.post "/blogs", body: blog_params | |
response.headers["Location"].should eq "/blogs" | |
response.status_code.should eq(302) | |
response.body.should eq "302" | |
end | |
it "updates a blog" do | |
Blog.clear | |
model = create_blog | |
response = subject.patch "/blogs/#{model.id}", body: blog_params | |
response.headers["Location"].should eq "/blogs" | |
response.status_code.should eq(302) | |
response.body.should eq "302" | |
end | |
it "deletes a blog" do | |
Blog.clear | |
model = create_blog | |
response = subject.delete "/blogs/#{model.id}" | |
response.headers["Location"].should eq "/blogs" | |
response.status_code.should eq(302) | |
response.body.should eq "302" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment