dsu1/Archivist

Implement Testing

Opened this issue · 2 comments

dsu1 commented

Like many pitfalls of programming, I skipped making tests for my code.

Time to fix that by implementing Rails tests and Yaml fixtures.

dsu1 commented

Tests Update 1

Testing infrastructure imported. Using Capybara, FactoryGirl, and Rspec.

Tests Update 2

Model tests added:

archive_spec.rb
require "rails_helper"

RSpec.describe Archive, :type => :model do

	it "is valid with valid archive attributes" do
		FactoryGirl.create(:user)
		expect(FactoryGirl.build(:archive)).to be_valid
	end

	it "is invalid without an archive name" do
		FactoryGirl.create(:user)
		expect(FactoryGirl.build(:archive, name: nil)).to_not be_valid
	end

# this test fails because there isn't a check for archive's user_id
	it "is an invalid archive without a user id" do
		# FactoryGirl.create(:user)
		# expect(FactoryGirl.build(:archive, user_id: nil)).to_not be_valid
	end

# this test fails because there isn't a check for archive's user_id
	it "is an invalid archive with a non-existant user id" do
		# expect(FactoryGirl.build(:archive, user_id: 2120)).to_not be_valid
	end


end
user_spec.rb
require "rails_helper"

RSpec.describe User, :type => :model do

	it "is valid with valid user attributes" do
		expect(FactoryGirl.build(:user)).to be_valid
	end

	it "is invalid without a username" do
		expect(FactoryGirl.build(:user, username: nil)).to_not be_valid
	end

	it "is invalid without an email" do
		expect(FactoryGirl.build(:user, email: nil)).to_not be_valid
	end

	it "is invalid without a password" do
		expect(FactoryGirl.build(:user, password: nil)).to_not be_valid
	end

	it "is invalid with duplicate usernames" do
		FactoryGirl.create(:user, username: "test_user1")
		expect(FactoryGirl.build(:user, username: "test_user1")).to_not be_valid
	end

	it "is invalid with duplicate emails" do
		FactoryGirl.create(:user, email: "test@user1.com")
		expect(FactoryGirl.build(:user, email: "test@user1.com")).to_not be_valid
	end

	it "is invalid with too short of a username" do
		expect(FactoryGirl.build(:user, username: "t")).to_not be_valid
	end

	# This test will fail because the implementation fails
	it "is invalid with too long of a username" do
		# expect(FactoryGirl.build(:user, username: "test_user_very_long_username_shouldn't_be_this_long")).to_not be_valid
	end

end
content_spec.rb
require "rails_helper"

RSpec.describe Archive, :type => :model do

	it "is valid with valid content attributes" do
		FactoryGirl.create(:archive)
		expect(FactoryGirl.build(:content)).to be_valid
	end

	it "is invalid without an archive name" do
		FactoryGirl.create(:archive)
		expect(FactoryGirl.build(:content, title: nil)).to_not be_valid
	end

end