diff options
| author | Kir | 2011-12-06 16:30:29 +0400 | 
|---|---|---|
| committer | Kir | 2011-12-06 16:30:29 +0400 | 
| commit | 3637c99f3e7508cc09cd0f87ed148047393a4ef7 (patch) | |
| tree | 9cd97f2a13d512768b8d5c531a4817404c72a95c | |
| parent | a90563c861d541b3e87b1b61160d4b11f450de5a (diff) | |
| download | inboxes-3637c99f3e7508cc09cd0f87ed148047393a4ef7.tar.bz2 | |
rspec (TODO: write fake_app.rb)
| -rw-r--r-- | .rspec | 1 | ||||
| -rw-r--r-- | Rakefile | 4 | ||||
| -rw-r--r-- | inboxes.gemspec | 5 | ||||
| -rw-r--r-- | spec/inboxes/discussion_spec.rb | 104 | ||||
| -rw-r--r-- | spec/inboxes/discussions_controller_spec.rb | 96 | ||||
| -rw-r--r-- | spec/inboxes/discussions_routing_spec.rb | 23 | ||||
| -rw-r--r-- | spec/inboxes/message_spec.rb | 27 | ||||
| -rw-r--r-- | spec/inboxes/messages_controller_spec.rb | 42 | ||||
| -rw-r--r-- | spec/inboxes/speaker_spec.rb | 16 | ||||
| -rw-r--r-- | spec/inboxes/speakers_controller_spec.rb | 72 | ||||
| -rw-r--r-- | spec/inboxes_spec.rb | 0 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 2 | ||||
| -rw-r--r-- | spec/support/factories.rb | 25 | 
13 files changed, 415 insertions, 2 deletions
| @@ -0,0 +1 @@ +--color @@ -1 +1,5 @@  require "bundler/gem_tasks" +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) +task :default => :spec
\ No newline at end of file diff --git a/inboxes.gemspec b/inboxes.gemspec index 5900502..9f7a2ba 100644 --- a/inboxes.gemspec +++ b/inboxes.gemspec @@ -25,8 +25,9 @@ Gem::Specification.new do |s|    # s.add_runtime_dependency "inherited_resources"    # s.add_development_dependency 'dm-sqlite-adapter', ['>= 1.1.0'] -  # s.add_development_dependency 'rspec', ['>= 0'] -  # s.add_development_dependency 'rspec-rails', ['>= 0'] +  s.add_development_dependency 'rspec', ['>= 0'] +  s.add_development_dependency 'factory_girl', ['~> 1.2'] +  s.add_development_dependency 'rspec-rails', ['>= 0']    # s.add_development_dependency 'rr', ['>= 0']    # s.add_development_dependency 'steak', ['>= 0']    # s.add_development_dependency 'capybara', ['>= 0'] diff --git a/spec/inboxes/discussion_spec.rb b/spec/inboxes/discussion_spec.rb new file mode 100644 index 0000000..e74e7b7 --- /dev/null +++ b/spec/inboxes/discussion_spec.rb @@ -0,0 +1,104 @@ +require 'spec_helper' + +describe Discussion do + +  it "should create valid group discussion" do +    discussion = Discussion.new +    discussion.recipient_ids = [Factory(:user), Factory(:user), Factory(:user)].map { |u| u.id } +    discussion.save.should be true +     +    discussion.users.count.should be == 3 +    # discussion.private?.should be false +  end +   +  it "should create valid private discussion" do +    discussion = Discussion.new +    discussion.recipient_ids = [Factory(:user).id, Factory(:user).id] +    discussion.save.should be true +    discussion.private?.should be true +  end +   +  it "should add messages to valid discussion" do +    discussion = Discussion.new +    discussion.recipient_ids = [Factory(:user), Factory(:user)].map { |u| u.id } +    discussion.save.should be true +     +    message = Message.new(:user => Factory(:user), :body => Factory.next(:string), :discussion => discussion) +    message.save.should be true +  end +   +  it "should not create discussion without repicients" do +    discussion = Discussion.new +    discussion.save.should be false +  end +   +  it "should not add speakers if discussion is not saved" do +    discussion = Discussion.new +    lambda { discussion.add_speaker(Factory(:user)) }.should raise_error(ArgumentError) +  end +   +  it "should add speakers to discussion" do +    discussion = Factory(:discussion) +    user = Factory(:user) +    discussion.add_speaker(user) +    discussion.users.should include user +  end +   +  it "should remove speaker from discussion" do +    discussion = Factory(:discussion) +    user = Factory(:user) +    discussion.add_speaker(user) +    discussion.remove_speaker(user) +     +    discussion.users.should_not include user +  end +   +  it "should assign discussion as viewed for user" do +    user = Factory(:user) +    discussion = Factory(:discussion) +    discussion.mark_as_read_for(user) +    discussion.unread_for?(user).should be false +  end +   +  it "model with new message should be unread for user" do +    discussion = Factory(:discussion) +    message = Message.create!(:user => Factory(:user), :body => Factory.next(:string), :discussion => discussion) +    discussion.unread_for?(Factory(:user)).should be true +  end +   +  it "attribute user_invited_at should be right" do +    discussion = Factory(:discussion) +    user = Factory(:user) +    speaker = discussion.add_speaker(user) +    speaker.created_at.to_i.should be == discussion.user_invited_at(user).to_i +  end +   +  it "method exists_between_users? should be right" do +    user = Factory(:user) +    user2 = Factory(:user) +    discussion = Discussion.create!(:recipient_ids => [user.id, user2.id]) +     +    Discussion.find_between_users(user, user2).should be == discussion +    Discussion.find_between_users(user2, user).should be == discussion +  end +   +  it "method exists_between_users? should be right if there is no discussion" do +    user = Factory(:user) +    user2 = Factory(:user) +     +    Discussion.find_between_users(user, user2).should be_nil +    Discussion.find_between_users(user2, user).should be_nil +  end +   +  it "method exists_between_users? should be right if 2 users are in group discussion" do +    user = Factory(:user) +    user2 = Factory(:user) +    user3 = Factory(:user) +    discussion = Discussion.create!(:recipient_ids => [user.id, user2.id, user3.id]) +     +    Discussion.find_between_users(user, user2).should be_nil +    Discussion.find_between_users(user2, user).should be_nil +    Discussion.find_between_users(user2, user3).should be_nil +  end +   +end diff --git a/spec/inboxes/discussions_controller_spec.rb b/spec/inboxes/discussions_controller_spec.rb new file mode 100644 index 0000000..564e9e8 --- /dev/null +++ b/spec/inboxes/discussions_controller_spec.rb @@ -0,0 +1,96 @@ +require 'spec_helper' + +describe Inboxes::DiscussionsController do +  context "Guest" do +    it "should not see discussions list" do +      get :index +      response.should redirect_to(sign_in_url) +    end +     +    it "should not see new action" do +      get :new +      response.should redirect_to(sign_in_url) +    end +     +    it "should not create discussion if model is valid" do +      recipient_ids = [Factory(:user).id, Factory(:user).id] +      post(:create, +        :discussion => { +          :recipient_ids => recipient_ids, +          :messages_attributes => { +            0 => {:body => Factory.next(:string)} +          } +        }  +      ) +       +      response.should redirect_to(sign_in_url) +    end + +  end +   +  context("Authenticated admin") do +    before(:each) do +      @request.env["devise.mapping"] = Devise.mappings[:user] +      @user = Factory(:user) +      @user.set_role(:admin) +      sign_in @user +    end +     +    it "should see discussions list" do +      get :index +      response.should render_template(:index) +    end +     +    it "should see new action" do +      get :new +      response.should render_template(:new) +    end +     +    it "should open discussion" do +      discussion = Factory.build(:discussion) +      discussion.recipient_ids = [@user, Factory(:user)].map { |u| u.id } +      discussion.save.should be true +       +      get(:show, :id => discussion) +      response.status.should be 200 +    end +     +    it "should create private discussion if model is valid" do +      recipient_ids = [Factory(:user).id, Factory(:user).id] +      post(:create, +        :discussion => { +          :recipient_ids => recipient_ids, +          :messages_attributes => { +            0 => {:body => Factory.next(:string)} +          } +        }  +      ) +       +      response.should redirect_to(discussion_url(assigns[:discussion])) +    end +     +    it "should create group discussion if model is valid" do +      recipient_ids = [Factory(:user).id, Factory(:user).id, Factory(:user).id] +      post(:create, +        :discussion => { +          :recipient_ids => recipient_ids, +          :messages_attributes => { +            0 => {:body => Factory.next(:string)} +          } +        }  +      ) +       +      response.should redirect_to(discussion_url(assigns[:discussion])) +    end +     +    it "should not create discussion with empty message" do +      discussion = Discussion.new +      discussion.recipient_ids = [Factory(:user).id, Factory(:user).id, Factory(:user).id] +      post(:create, :discussion => discussion) +       +      response.should render_template(:new) +    end +     +  end + +end diff --git a/spec/inboxes/discussions_routing_spec.rb b/spec/inboxes/discussions_routing_spec.rb new file mode 100644 index 0000000..324cbe3 --- /dev/null +++ b/spec/inboxes/discussions_routing_spec.rb @@ -0,0 +1,23 @@ +require "spec_helper" + +describe Inboxes::DiscussionsController do +  describe "routing" do + +    it "routes to #index" do +      get("/discussions").should route_to("inboxes/discussions#index") +    end + +    it "routes to #new" do +      get("/discussions/new").should route_to("inboxes/discussions#new") +    end + +    it "routes to #show" do +      get("/discussions/1").should route_to("inboxes/discussions#show", :id => "1") +    end + +    it "routes to #create" do +      post("/discussions").should route_to("inboxes/discussions#create") +    end + +  end +end diff --git a/spec/inboxes/message_spec.rb b/spec/inboxes/message_spec.rb new file mode 100644 index 0000000..c498b04 --- /dev/null +++ b/spec/inboxes/message_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe Message do +  it "should not be visible to new speaker" do +    discussion = Factory(:discussion) +    old_user = Factory(:user) +    discussion.add_speaker(old_user) +    message = Message.create!(:discussion => discussion, :user => old_user, :body => Factory.next(:string)) +     +    sleep 2 +     +    new_user = Factory(:user) +    discussion.add_speaker(new_user) +    message.visible_for?(new_user).should be_false +  end +   +  it "should be visible to old speaker" do +    discussion = Factory(:discussion) +    user = Factory(:user) +    discussion.add_speaker(user) +    message = Message.create!(:discussion => discussion, :user => user, :body => Factory.next(:string)) +     +    sleep 5 +     +    message.visible_for?(user).should be_true +  end +end diff --git a/spec/inboxes/messages_controller_spec.rb b/spec/inboxes/messages_controller_spec.rb new file mode 100644 index 0000000..0c63070 --- /dev/null +++ b/spec/inboxes/messages_controller_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Inboxes::MessagesController do + +  render_views +   +  context "Guest" do +    it "should redirect guest if he wants to create message" do +      discussion = Factory(:discussion) +      # puts discussion.id +      post :create, :discussion_id => discussion.id +      response.should redirect_to(sign_in_url) +    end +  end +   +  context "Authenticated admin" do +    before(:each) do +      @request.env["devise.mapping"] = Devise.mappings[:user] +      @admin = Factory(:admin) +      @admin.set_role(:admin) +      sign_in @admin +    end + +    it "create action should redirect to discussion when model is valid" do +      Message.any_instance.stubs(:valid?).returns(true) +      message = Factory(:message) +      user = Factory(:user) +      discussion = Factory(:discussion, :recipient_ids => [@admin.id, user.id]) +      post(:create, :discussion_id => discussion.id) +      response.should redirect_to(discussion_url(discussion)) +    end + +    # it "create action should assign flash with error message" do +    #   Comment.any_instance.stubs(:valid?).returns(false) +    #   first_post = Factory(:post) +    #   post :create, :post_id => first_post +    #  +    #   flash[:notice].should =~ /Введите текст комментария!/i +    # end +  end +   +end diff --git a/spec/inboxes/speaker_spec.rb b/spec/inboxes/speaker_spec.rb new file mode 100644 index 0000000..22b4bdc --- /dev/null +++ b/spec/inboxes/speaker_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Speaker do +  # it "should create valid model" do +  #   speaker = Speaker.new +  #   speaker.user = Factory(:user) +  #   speaker.discussion = Factory(:discussion) +  #   speaker.save.should be true +  # end +  #  +  # it "should bot create model without discussion" do +  #   speaker = Speaker.new +  #   speaker.user = Factory(:user) +  #   speaker.save.should be false +  # end +end diff --git a/spec/inboxes/speakers_controller_spec.rb b/spec/inboxes/speakers_controller_spec.rb new file mode 100644 index 0000000..ce413d7 --- /dev/null +++ b/spec/inboxes/speakers_controller_spec.rb @@ -0,0 +1,72 @@ +require 'spec_helper' + +describe Inboxes::SpeakersController do + +  context("Authenticated admin") do +    before(:each) do +      @request.env["devise.mapping"] = Devise.mappings[:user] +      @user = Factory(:user) +      @user.set_role(:admin) +      sign_in @user +    end + +    it "should add speaker to discussion" do +      discussion = Factory(:discussion, :recipient_ids => [@user.id, Factory(:user).id]) +      post(:create, +        :discussion_id => discussion, +        :speaker => { +          :user_id => Factory(:user).id +        } +      ) +      response.should redirect_to(discussion_url(discussion)) +      flash[:notice].should =~ /Собеседник успешно добавлен/i +    end +     +    # it "should not add bad speaker to discussion" do +    #   discussion = Factory(:discussion) +    #    +    #   lambda { +    #     post(:create, +    #       :discussion_id => discussion +    #     ) +    #   }.should raise_error(ActiveRecord::RecordNotFound) +    # end +  end +   +  context("User") do +    before(:each) do +      @request.env["devise.mapping"] = Devise.mappings[:user] +      @user = Factory(:user) +      @user.set_role(:user) +      sign_in @user +    end +     +    it "should add speaker to discussion if he is participant if this discussion" do +      Speaker.any_instance.stubs(:valid?).returns(true) +      discussion = Factory(:discussion, :recipient_ids => [@user.id, Factory(:user).id]) +      # puts discussion.can_participate?(@user) +      # new_user = Factory(:user) +      post(:create, +        :discussion_id => discussion, +        :speaker => { +          :user_id => Factory(:user).id +        } +      ) +      response.should redirect_to(discussion_url(discussion)) +      # response.should redirect_to(root_url) +      flash[:notice].should =~ /Собеседник успешно добавлен/i +    end +     +    # дописать спек +    it "should not add speaker to discussion if he is not participant if this discussion" do +      discussion = Factory(:discussion) +      post(:create, +        :discussion_id => discussion, +        :speaker => { +          :user_id => Factory(:user).id +        } +      ) +      response.should redirect_to(root_url) +    end +  end +end diff --git a/spec/inboxes_spec.rb b/spec/inboxes_spec.rb new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/spec/inboxes_spec.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..174ab57 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,2 @@ +require 'inboxes' + diff --git a/spec/support/factories.rb b/spec/support/factories.rb new file mode 100644 index 0000000..a7eaad6 --- /dev/null +++ b/spec/support/factories.rb @@ -0,0 +1,25 @@ +FactoryGirl.define do +   +  factory :user do +    email {Factory.next(:email)} +    first_name 'user' +    last_name 'usered' +    username {Factory.next(:login)} +    password "foobar" +    password_confirmation { |u| u.password } +    role 2 +  end + +  factory :discussion do +    recipient_ids {[Factory(:user).id, Factory(:user).id]} +  end + +  factory :message do +    association :user +    association :discussion +    # user {Factory(:user)} +    # discussion {Factory(:discussion)} +  end + + +end
\ No newline at end of file | 
