你的用戶對象上獨立測試應該更多這樣的:
it "should have an email attribute" do
@attr.should respond_to(:email) }
end
不過,如果你正在測試一個類,可能我建議沿着這爲您的測試線的東西更多:
before (:each) do
@attr = {name: "One", email: "[email protected]"}
end
那麼你的第一次測試,確保創建工作,像這樣:
it "should create a new instance given valid attributes" do
Employee.create!(@attr)
end
一旦you'v Ë確立的工作你就可以繼續你的其他測試,並有自動獲得像這樣創建的員工:
describe "Attributes and method tests" do
before (:each) do
@employee = Employee.create(@attr)
end
it "should have a name attribute" do
@employee.should respond_to(:name)
end
it "should have an email attribute" do
@employee.should respond_to(:email) }
end
# etc... (to test other attributes or methods)
end
要獲得關於扭轉測試,並指定這不是有效的,如果屬性是空白的問題,我會嘗試這樣的事情。
it "should not be valid with a blank email" do
Employee.new(@attr.merge(:email => '').should_not be_valid
end
it "should not be valid with a blank name" do
Employee.new(@attr.merge(:name => '').should_not be_valid
end
你究竟想要在這裏測試什麼?測試某些方法是否響應並測試該方法的返回值是否爲空是不相交的概念。 – 2012-02-02 21:04:42