require 'rails_helper'

describe LessonCompletion do

  let!(:user){ FactoryGirl.create(:user) }
  let!(:lesson){ FactoryGirl.create(:lesson) }

  subject(:lesson_completion) { LessonCompletion.new(
    :student_id => user.id,
    :lesson_id => lesson.id
                               )}

  it { is_expected.to respond_to(:student) }
  it { is_expected.to respond_to(:lesson) }
  it { is_expected.to be_valid }

  context "when lesson and student are duplicated" do
    before do
      LessonCompletion.create(:student_id => user.id, :lesson_id => lesson.id)
    end
    it { is_expected.not_to be_valid }
  end

  # Also test that they delete when the lesson or user are deleted?

end

require 'rails_helper'

describe LessonCompletion do

  let!(:user)  { double("User",    :id => 1) }
  let!(:lesson){ double("Lessons", :id => 1) }

  subject(:lesson_completion) { LessonCompletion.new(
    :student_id => user.id,
    :lesson_id => lesson.id
  )}

  it { is_expected.to be_valid }
  it { is_expected.to respond_to(:student) }
  it { is_expected.to respond_to(:lesson) }
  it { is_expected.to validate_presence_of(:student_id) }
  it { is_expected.to validate_presence_of(:lesson_id) }
  it { is_expected.to validate_uniqueness_of(:student_id).scoped_to(:lesson_id) }
  it { is_expected.to belong_to(:student) }
  it { is_expected.to belong_to(:lesson) }

  # Also test that they delete when the lesson or user are deleted?

end

Refactor lesson completion model spec file
Refactor lesson completion model spec file

Addresses Issue 262

I replaced the two FactoryGirl variables with doubles, added shoulda matchers to test the validations and associations, and removed the last test. I did not address the existing comment about testing that the lesson completion is deleted when a lesson or user is deleted—slightly beyond my scope of knowledge.
