RSpec :: Core :: ExampleGroup :: Nested_2中的未定义方法`it'

时间:2013-07-23 10:22:38

标签: ruby-on-rails http rspec rspec-rails fakeweb

当我尝试实施“Fakeweb”并且我不明白这个问题时,我遇到了这个错误。

方案: “在用户填写完所有信息后,系统将使用其中一个属性”fbid“进行验证,如果成功则只创建一个新公司,否则进程将失败。”

Failures:
1) Companies new company create with valid information correct facebook id validates facebook id
 Failure/Error: it "should create a company" do
 NoMethodError:
   undefined method `it' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_3::Nested_1::Nested_1:0x00000102fb84e0>
 # ./spec/requests/companies_spec.rb:40:in `block (5 levels) in <top (required)>'

companies_spec.rb

    describe "correct facebook id" do               
        #validate fbid
        it "validates facebook id" do
            FakeWeb.register_uri(:head, "http://graph.facebook.com/examplecompany", :username => 'examplecompany')
            Company.new(:url => "http://graph.facebook.com/examplecompany").fb_id_string.should eq('examplecompany')

            it "should create a company" do 
                expect { click_button submit }.to change(Company, :count).by(1)
            end

模型/ company.rb

def fb_id_string
    uri = URI.parse(url)
    response = Net::HTTP.start(uri.host, uri.port) { |http| http.request_head(uri.path) }
    response["username"].to_str
end    

结束

1 个答案:

答案 0 :(得分:4)

由于谷歌搜索rspec nested it并没有产生任何结果,我想我会详细说明@ apneadiving的评论。

虽然rspec允许describe及其同义词被任意嵌套,而it在传递字符串参数时在其结构中类似于describe,但it块可以只包含模拟,期望,应用程序代码和vanilla Ruby。它不能包含it的其他调用,或者beforeafterlet的其他调用。它可以调用subject,但这样做只会调用主题被定义为的块;它不会重新定义主题。

相反,嘲笑和期望不能直接在describe内表达。它们必须包含在it内。