<commit_before>require 'spec_helper'

# TODO: demonstrate better tests tomorrow
describe User do

  before(:each) do
    @user1 = Factory.build(:user)
    @user2 = Factory.build(:user)
  end

  def valid_attributes
    {name: "Peter", email: "<EMAIL>"}
  end

  subject{@user1}

  it{should respond_to :name}
  it{should respond_to :email}
  it{should respond_to :description}

  it "can be created with valid attributes" do
    User.new(valid_attributes).should be_valid
  end

  it "is invalid without a name" do
    User.new(valid_attributes.merge(name: nil)).should_not be_valid
  end

  context "when name is not given" do
    before{@user1.name = nil}
    after{@user1.name = @user2.name}
    it{should_not be_valid}
  end

  context "when email is not given" do
    before{@user1.email = nil}
    after{@user1.email = @user2.email}
    it{should_not be_valid}
  end

  context "when email has not the correct format" do
    before{@user1.email = "florian"}
    after{@user1.email = @user2.email}
    it{should_not be_valid}
  end

  context "when email is already taken by an other user" do
    before{@user2.save}
    after{@user2.destroy}
    it{should_not be_valid}
  end

  describe "Role" do
    before :all do
      @role = Factory.build(:role)
    end

    it "should have empty roles upon creation" do
      @user1.roles.should be_empty
    end

    it "can have a role assigned" do
      @user1.roles << @role
      @user1.roles.size.should == 1
      @user1.should be_valid
    end

    it "can have multiple roles assigned" do
      @user1.roles << @role << Factory.build(:other_role)
      @user1.roles.size.should == 2
    end

  end

end<commit_msg>A bit of userspec refactoring<commit_after>require 'spec_helper'

# TODO: demonstrate better tests tomorrow
describe User do

  before(:each) do
    @user = Factory.build(:user)
  end

  subject{@user}

  it{should respond_to :name}
  it{should respond_to :email}
  it{should respond_to :description}

  context "given valid attributes" do
    it { should be_valid }
  end

  context "without a name" do
    before { @user.name = nil }
    it { should_not be_valid }
  end

  context "without an email" do
    before { @user.email = nil }
    it { should_not be_valid }
  end


  context "when email has not the correct format" do
    before{@user.email = "florian"}
    it{should_not be_valid}
  end

  context "when email is already taken by an other user" do
    before{Factory(:user)}
    it{should_not be_valid}
  end

  describe "Role" do
    before :all do
      @role = Factory.build(:role)
    end

    it "should have empty roles upon creation" do
      @user.roles.should be_empty
    end

    it "can have a role assigned" do
      @user.roles << @role
      @user.roles.size.should == 1
      @user.should be_valid
    end

    it "can have multiple roles assigned" do
      @user.roles << @role << Factory.build(:other_role)
      @user.roles.size.should == 2
    end

  end

end
{'fraction_non_alphanumeric': 0.09753628079649004, 'fraction_numerical': 0.00809989875126561, 'mean_word_length': 2.9154557463672393, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3916307', 'n_tokens_mistral': 1014, 'n_tokens_neox': 974, 'n_words': 304}