Here's a fairly general macro:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\countsubstringtestTF}{mmmmm}
{% #1 = substring to test for, #2 = string, #3 = test, #4 = true text, #5 = false text
\seq_set_split:Nnn \l__salys_substring_seq { #1 } { #2 }
\cs_set:Nn \__salys_substring_test:n
{
\int_compare:nTF { #3 } { #4 } { #5 }
}
\__salys_substring_test:n { \seq_count:N \l__salys_substring_seq - 1 }
}
\seq_new:N \l__salys_substring_seq
\ExplSyntaxOff
\begin{document}
\countsubstringtestTF{~}{This is a test}{#1>2}{greater than 2}{not greater than 2}
\countsubstringtestTF{~}{This~is a~test}{#1>2}{greater than 2}{not greater than 2}
\countsubstringtestTF{~}{This~is~a~test}{#1>2}{greater than 2}{not greater than 2}
\countsubstringtestTF{~}{This is a test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}
\countsubstringtestTF{~}{This~is a~test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}
\countsubstringtestTF{~}{This~is~a~test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}
\end{document}
The number of matches is denoted, in the third argument, by #1; you can do several sorts of comparisons, like the ones shown here.

If you want to allow the second argument to be a control sequence, you can define a variant.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\countsubstringtestTF}{smmmmm}
{
\IfBooleanTF { #1 }
{
\salys_countsubstringtest:nVnTF #2 { #3 } { #4 } { #5 } { #6 }
}
{
\salys_countsubstringtest:nnnTF { #2 } { #3 } { #4 } { #5 } { #6 }
}
}
\cs_new_protected:Nn \salys_countsubstringtest:nnnTF
{% #1 = substring to test for, #2 = string, #3 = test, #4 = true text, #5 = false text
\seq_set_split:Nnn \l__salys_substring_seq { #1 } { #2 }
\cs_set:Nn \__salys_substring_test:n
{
\int_compare:nTF { #3 } { #4 } { #5 }
}
\__salys_substring_test:n { \seq_count:N \l__salys_substring_seq - 1 }
}
\seq_new:N \l__salys_substring_seq
\cs_generate_variant:Nn \salys_countsubstringtest:nnnTF { nV }
\ExplSyntaxOff
\newcommand{\mystringA}{This~is a~test}
\newcommand{\mystringB}{This~is~a~test}
\begin{document}
\countsubstringtestTF{~}{This is a test}{#1>2}{greater than 2}{not greater than 2}
\countsubstringtestTF*{~}{\mystringA}{#1>2}{greater than 2}{not greater than 2}
\countsubstringtestTF*{~}{\mystringB}{#1>2}{greater than 2}{not greater than 2}
\countsubstringtestTF{~}{This is a test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}
\countsubstringtestTF{~}{This~is a~test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}
\countsubstringtestTF{~}{This~is~a~test}{1 <= #1 <= 2}{between 1 and 2}{not between 1 and 2}
\end{document}