2
我對RSpec和FactoryGirl來說很新,並且試圖讓我的測試在使用RSpec的工廠時通過。當使用FactoryGirl的序列時驗證失敗
我有一個規範/控制器/ shares_controller_spec.rb規格如下所示:
require 'spec_helper'
describe SharesController do
let(:user) do
user = FactoryGirl.create(:user)
user
end
let(:share) do
share = FactoryGirl.create(:share)
share
end
context "standard users" do
it "cannot remove other person's shares" do
sign_in(:user, user)
send('delete', 'destroy', :id => share.id)
response.should redirect_to shares_path
flash[:alert].should eql('You must be the author to delete this share.')
end
end
end
和投機/ factories.rb:
FactoryGirl.define do
factory :user do
sequence(:email) {|n| "user-#{n}@qwerty.com"}
password "password"
password_confirmation "password"
end
factory :share do
title "Test"
content "Test"
user FactoryGirl.create(:user)
end
end
當我運行
rspec spec/controllers/shares_controller_spec.rb我的測試但它不知何故打破了黃瓜:
$ rake cucumber:ok rake aborted! Validation failed: Email has already been taken Tasks: TOP => cucumber:ok => db:test:prepare => db:abort_if_pending_migrations => environment (See full trace by running task with --trace)
我在做什麼錯? 在此先感謝。
它的工作,非常感謝你!用戶關聯自動生成並不明顯 - 看起來像我錯過了文檔中的這一部分。再次感謝! –
不客氣。新的FactoryGirl語法起初有點不直觀,但對於大多數基本用例而言,它大大簡化了工廠代碼。 –