我正在尝试在rspec中测试我的sinatra应用程序(更具体地说,padrino应用程序)主页上的重定向。我找到了redirect_to,但它似乎只在rspec-rails中。你如何在sinatra中测试它?所以基本上,我想要这样的东西:it"Homepageshouldredirecttolocations#index"doget"/"last_response.shouldbe_redirect#Thisworks,butIwantittobemorespecific#last_response.shouldredirect_to('/locations')#Onlyworksf
考虑以下RSpec片段:it"shouldmatch"do{:a=>1,:b=>2}.should=~{"a"=>1,"b"=>2}end此测试失败,因为一个散列使用符号作为键,而另一个使用字符串作为键。在我的例子中,一个散列是一个解析的JSON对象,另一个是创建该对象的散列。我希望他们能平等地进行比较。在我编写自己的匹配器或强制两个哈希都具有字符串键之前,是否有匹配器或技术可以处理这种(常见)情况? 最佳答案 你可以这样做:it"shouldmatch"do{:a=>1,:b=>2}.stringify_keys.should=~
我正在学习rails3tutorial,但在进行如下测试时,我不明白“it”关键字的含义:require'spec_helper'describeUsersControllerdorender_viewsdescribe"GET'new'"doit"shouldbesuccessful"doget'new'response.shouldbe_successendit"shouldhavetherighttitle"doget'new'response.shouldhave_selector("title",:content=>"Signup")endendend代码片段来自:http:
我的模型有default_scope(:order=>'created_at')我的测试(rspec、factorygirl、shoulda等)是:require'spec/spec_helper.rb'describeCatMembershipdocontext"isvalid"dosubject{Factory.build(:cat_membership)}it{shouldbe_valid}it{shouldbelong_to:cat}it{shouldbelong_to:cat_group}it{shouldhave_db_column(:start_date)}it{shou
railstutorial.org有一个让我觉得有点奇怪的建议。Itsuggeststhiscode:classApplicationControllerincludeSessionsHelper使方法在ApplicationController中可用,是的,但它也使它们在任何View中都可用。我知道身份验证/授权是交叉的,但这真的是最好的地方吗?在我看来,这可能范围太广了。将实现有条件重定向(如railstutorial.org示例所做的)的before_filter的代码放在更通常包含View助手的模块中似乎令人惊讶。将View中不需要的功能放在ApplicationControl
据我了解,equal方法检查对象是否相同。person=Person.create!(:name=>"David")Person.find_by_name("David").shouldequal(person)这应该是真的。但是这里不是有两个不同的对象吗?两个对象怎么可能相同?我不明白。 最佳答案 Rails和RSpec等式测试有多种选择。Rails3.2ActiveRecord::Base使用==等式匹配器。它以两种不同的方式返回真值:如果self与比较对象完全相同如果self与比较对象是同一类型且具有相同的ID注意Active
什么是可靠的规范?这就是我发现关于测试非常抽象的地方。我会对模型、Controller和其他任何可以测试的问题的答案感兴趣。有一个规范的规范会很酷,你知道我的意思吗?模型规范应该(按优先级和相关性顺序):测试所有方法?测试错误数组?测试CRUD(以及如何)?还有什么?Controller/View规范应该(按优先级/相关性顺序):填空...?扩展规范应该包含和不应该包含的内容的列表会很棒。我还想编制一份技巧和建议列表。例如:关键字“应该”有点多余。示例:这个:it"shouldbeinvalidwithoutafirstname"会更好:it"isinvalidwithoutafirs
classFoodefbar(a,b)...Foo.should_receive(:bar)期望使用任何参数调用bar。Foo.should_receive(:bar).with(:baz,:qux)期望:baz和:qux作为参数传入。如何期望第一个参数等于:baz,而不关心其他参数? 最佳答案 使用anything匹配器:Foo.should_receive(:bar).with(:baz,anything) 关于ruby-如何使用RSpecshould_receive期待一些(但不是
我正在使用Rspec测试我的ActiveRecord模型。我刚刚向我的验证之一添加了自定义错误消息,如下所示:validates:accepted_terms_at,:presence=>{:message=>'YoumustaccepttheTermsandConditionstousethissite.'}现在下面的测试失败了:it{shouldvalidate_presence_of(:accepted_terms_at)}...错误Expectederrorstoinclude"can'tbeblank"whenaccepted_terms_atissettonil。所以测试失
我在这里学习Rails教程:http://railstutorial.org/chapters/filling-in-the-layout#top当我运行“rspecspec/”时,我得到一堆如下所示的错误:1)LayoutLinksshouldhaveaHomepageat'/'Failure/Error:Unabletofindmatchinglinefrombacktracestackleveltoodeep#C:/Ruby19/lib/ruby/1.9.1/forwardable.rb:1852)LayoutLinksshouldhaveaContactpageat'/cont