【问题标题】:How to create an article of the user如何创建用户的文章
【发布时间】:2013-04-24 13:35:42
【问题描述】:

我正在使用 RSpec、FactoryGirls 测试控制器。
这是我的工厂.rb

FactoryGirl.define do
  factory :user do |user|
    user.sequence(:name) { Faker::Internet.user_name }
    user.email Faker::Internet.email
    user.password "password"
    user.password_confirmation "password"
  end

  factory :article do
    user
    title Faker::Lorem.sentence(5)
    content Faker::Lorem.paragraph(20)
  end
end

如何在此处创建用户的文章
这是articles_controller_spec

 describe ArticlesController do
      let(:user) do
        user = FactoryGirl.create(:user)
        user.confirm!
        user
      end

      describe "GET #index" do
        it "populates an array of articles of the user" do
          #how can i create an article of the user here
          sign_in user
          get :index
          assigns(:articles).should eq([article])
        end

        it "renders the :index view" do
          get :index
          response.should render_template :index
        end
      end
    end

【问题讨论】:

  • 你想测试createArticlesController的动作吗?
  • 否,ArticlesController 的索引操作
  • 查看我的更新答案
  • 首先我需要创建一篇文章然后检查,你在哪里创建了用户的文章?
  • 您的index 操作正在创建文章?

标签: ruby-on-rails bdd factory-bot rspec-rails


【解决方案1】:

旧版本,而不是特征,是这样的:

describe ArticlesController do

  ..

  describe "GET #index" do
    it "populates an array of articles of the user" do

      article = FactoryGirl.create(:article, :user => user)

      sign_in user
      get :index
      assigns(:articles).should eq([article])
    end

  ..

end

【讨论】:

    【解决方案2】:
     describe ArticlesController do
        let(:user) do
          user = FactoryGirl.create(:user)
          user.confirm!
          user
      end
    
       describe "GET #index" do
        it "populates an array of articles of the user" do
          #how can i create an article of the user here
          sign_in user
          get :index
          assigns(:articles).should eq([article])
        end
    
        it "renders the :index view" do
          get :index
          response.should render_template :index
        end
    
         it "assign all atricles to @atricles" do
           get :index
           assigns(:atricles).your_awesome_test_check #  assigns(:articles) would give you access to instance variable
         end
      end
    end
    

    【讨论】:

      【解决方案3】:

      你可以指定一个已经有文章的用户工厂

      FactoryGirl.define do
        factory :user do |user|
          user.sequence(:name) { Faker::Internet.user_name }
          user.email Faker::Internet.email
          user.password "password"
          user.password_confirmation "password"
        end
      
        factory :article do
          user
          title Faker::Lorem.sentence(5)
          content Faker::Lorem.paragraph(20)
        end
      
        trait :with_articles do
          after :create do |user|
            FactoryGirl.create_list :article, 2, :user => user
          end
        end
      end
      

      然后在你的控制器测试中

      FactoryGirl.create :user, :with_articles # => returns user with 2 articles
      

      更新

      我认为您希望查看每个用户的所有文章.. 如果是这样的话,请使用

      get :index, {:id => user.id}

      这样您就可以在控制器中查找用户并获取所有文章

      @user = User.find(params[:id]);
      @articles = @user.articles
      

      如果不是这样,那就做吧

      @articles =  Article.all
      

      使用trait :with_articles后应该至少显示2个Articles

      你可以用一个简单的断言来测试它 期望(@article.size).to eq(2)

      【讨论】:

      • 如何在 controller_spec 中获取创建的文章?我的意思是分配给一个变量
      • 在 let 块中.. 而不是使用 FactoryGirl.create(:user) 您使用 FactoryGirl.create :user, :with_articles.. 并且您必须将用户 ID 作为参数传递.. `get :index, {:id => user .id}(这可能是你想要的)
      猜你喜欢
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多