【发布时间】: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