2013-04-24 23 views
1

我正在用RSpec,FactoryGirls測試控制器。
這是我factories.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 
+0

你想測試'ArticlesController'的'create'動作? – Naveed 2013-04-24 14:08:48

+0

不,索引行動ArticlesController – 2013-04-24 14:16:20

+0

看到我更新的答案 – Naveed 2013-04-24 14:21:50

回答

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 
1

你可以指定一個用戶出廠時已物品

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 

UPDATE

我想你想看到每個用戶的所有文章..如果多數民衆贊成的情況下使用

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

這樣您要查找的用戶即可獲得所有文章您的控制器

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

如果不是這樣,那麼只是使用trait :with_articles應顯示後做

@articles = Article.all 

至少2 Articles

你可以用一個簡單的斷言像 預期(@ article.size)。爲了EQ(2)

+0

如何獲取在controller_spec中創建的文章?我的意思是在let塊中分配一個變量 – 2013-04-24 14:07:57

+0

..而不是使用'FactoryGirl.create(:user)'你使用'FactoryGirl.create:user,:with_articles' ..並且你必須傳遞用戶ID作爲params。 。get:index,{:id => user.id}(這可能是你想要的) – Orlando 2013-04-24 15:35:01

1
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 
測試這個