【发布时间】:2011-05-10 23:50:31
【问题描述】:
我对编程、Rails、Ruby、Rspec 等比较陌生,所以感谢您的帮助!
我的规范非常重复,所以我编写了一些规范辅助方法。我无法弄清楚如何在我的规格中正确使用它们。具体来说,我有一个带有 create 的用户控制器:
def create
@user = User.new(params[:user])
if @user.save
redirect_to user_path(@user)
else
render :action => :new
end
end
创建有效用户的规范助手中的一点:
def valid_user_eilif
@test_image = Rails.root + "spec/fixtures/images/seagull.jpg"
@file = Rack::Test::UploadedFile.new(@test_image, "image/jpeg")
user = User.create!(:username => "eilif", :email => "eilif@email.org",
:image => @file, :bio => "Lots of text that I don't want to write",
:signature_quote => "Yet more text.")
user.save!
user
end
然后在我的用户控制器规范中:
before (:each) do
post :create, :user => valid_user_eilif
end
it 'should assign user to @user' do
assigns(:user).should eq(User.last)
end
当我运行规范时,我得到了错误:
Failure/Error: assigns(:user).should eq(User.last)
expected #<User id: 1, username: "eilif", email: "eilif@email.org", bio: "Lots of text that I don't want to write", signature_quote: "I feel empty.", image_file_name: "seagull.jpg", image_content_type: "image/jpeg", image_file_size: 10475, image_updated_at: "2011-05-10 23:35:55", created_at: "2011-05-10 23:35:56", updated_at: "2011-05-10 23:35:56">
got #<User id: nil, username: nil, email: nil, bio: nil, signature_quote: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, created_at: nil, updated_at: nil>
所以,我假设我错误地发布创建,因为没有创建任何东西?这样做的正确方法是什么?
【问题讨论】:
标签: ruby-on-rails rspec helper specifications