期待GET动作返回一个元素,返回许多元素

时间:2014-01-25 04:26:44

标签: ruby-on-rails testing ruby-on-rails-4

我有以下控制器规范,并期望主题只包含一个元素(因为DatabaseCleaner会进行清理),除了我有多个主题。

  describe "GET index" do
    it "assigns all topics as @topics" do
      topic = FactoryGirl.create(:topic)
      get :index, {}
      assigns(:topics).should eq([topic])
    end
  end

这是我的数据库清理器配置 -

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
    DatabaseCleaner.clean 
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  config.before(:all) do
    DeferredGarbageCollection.start
  end

  config.after(:all) do
    Defer
  end

而且,这是主题的工厂 -

  factory :topic do
    sequence(:heading) { |seq| "Heading For Topic - #{seq}" }
    sequence(:slug) { |seq| "slug-for-topic-#{seq}" }
    sequence(:title) { |seq| "Title for Topic #{seq}" }
    sequence(:description) { |seq| "Description for Topic #{seq}" }
    sequence(:content) { |seq| "Content for Topic #{seq}" }
    sequence(:link) { |seq| "Link for Topic #{seq}" }

    site
    # User should belong to site
    user { association :user, site_id: site.id }
    end

为什么在GET操作之前没有清理数据库?

更新

索引控制器(这里没什么特别的)

  def index
    @topics = Topic.all.limit 10

    respond_to do |format|
      format.html
      format.json { rendor json: @topics }
    end
  end

我的测试文件不完整:

describe TopicsController do
  describe "GET index" do
    it "assigns all topics as @topics" do
      topic = FactoryGirl.create(:topic)
      get :index, {}
      assigns(:topics).should eq([topic])
    end
  end

  describe "GET show" do
    it "assigns topic as @topic" do
      topic = FactoryGirl.create(:topic)
      get :show, {slug: topic[:slug]}
      assigns(:topic).should eq(topic)
    end

    it "should redirect to 404 if topic is not found" do
      expect {
        get :show, {slug: "non-existant-topic"}
      }.to raise_error(ActionController::RoutingError)
    end
  end

  describe "GET new" do
    describe "autheticated user" do
      before :each do

        @user = FactoryGirl.create :user
        sign_in @user
      end
      it "assigns a new topic as @topic" do
        get :new, {}
        assigns(:topic).should be_a_new(Topic)
      end
    end

    describe "unauthenticated user" do
      it "should redirect to sign_up page" do
        get :new, {}
        response.should redirect_to new_user_session_path
      end
    end
  end

  describe "POST create" do
    describe "with valid params" do
      before :each do
        @user = FactoryGirl.create :user
        sign_in @user
      end

      it "assigns a newly created topic as @topic" do
        topic = FactoryGirl.build(:topic)
        post :create, {topic: topic.attributes}
        assigns(:topic).should be_a(Topic)
        assigns(:topic).should be_persisted
      end

      it "redirects to the created topic" do
        topic = FactoryGirl.build(:topic)
        post :create, {topic: topic.attributes}
        response.should redirect_to(topic_path(Topic.last.slug))
      end
    end

    describe "with invalid params" do
      before :each do
        @user = FactoryGirl.create :user
        sign_in @user
      end
      it "assigns a newly created but unsaved topic as @topic" do
        post :create, { topic: Topic.new.attributes }
        assigns(:topic).should be_a_new(Topic)
      end

      it "re-renders the 'new' template" do
        post :create, {topic: Topic.new.attributes }
        response.should render_template("new")
      end
    end

    describe "unautheticated" do
      # before :each do
      #   sign_out :user
      # end
      it "should fail" do
        topic = FactoryGirl.build(:topic)
        post :create, { topic: topic.attributes }
        response.should redirect_to new_user_session_path
      end
    end
  end

0 个答案:

没有答案
相关问题