1
嘿,我完全失去了这一个。指定手动呼叫有效?
我在网上找到一个代码片段,以帮助验证通过ajax字段,因为用户键入他们。所以我试图写一个规范来反对它的一部分,我无法让它通过。
下面的代码
def validate
field = params[:field]
user = User.new(field => params[:value])
output = ""
user.valid?
if user.errors[field] != nil
if user.errors[field].class == String
output = "#{field.titleize} #{user.errors[field]}"
else
output = "#{field.titleize} #{user.errors[field].to_sentence}"
end
end
render :text => output
end
,这里是我的测试,到目前为止
describe "POST validate" do
it "retrieves the user based on the past in username" do
mock_errors ||= mock("errors")
mock_errors.stub!(:[]).and_return(nil)
User.should_receive(:new).with({'username'=>"UserName"}).and_return(mock_user)
mock_user.should_receive(:valid?).and_return(true)
mock_errors.should_receive(:[]).with("username").and_return(nil)
put :validate, :field=>'username', :value=>'UserName'
response.should == ""
end
end
我得到这个错误 -
1)规格::嘲笑:: MockExpectationError 在'UsersController POST验证 根据我们过去的 检索用户ername”模拟‘错误’收到 意外的消息:[]与 (‘用户名’)
我似乎无法弄清楚如何在世界嘲笑调用user.errors [场]。理想情况下,这个规范测试了快乐的道路,没有错误。然后我会写另一个验证失败。